PDA

View Full Version : How to pass a normal datatype argument to an array parameter?



prabhafriend
06-17-2011, 02:50 AM
Hi,

I have an array parameter named listofiles() but sometimes I need to pass a single file too. How to pass it to an array parameter?
P.S:

Is it possible to pass the 'List of items' in a listbox as an array arugment to an array parameter directly?

Kenneth Hobs
06-17-2011, 05:28 AM
In a userform, add a listbox and a command button. Paste this in the userform's code and play the first sub. You will see the range A1:A10 filled and the listbox. Click the command button to see the value of each element in the array which matches the listbox contents.
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Range("A" & i).Value2 = i
Next i
ListBox1.RowSource = "A1:A10"
End Sub

Private Sub CommandButton1_Click()
Dim i As Integer, a() As Variant
a() = WorksheetFunction.Transpose(ListBox1.List)
For i = LBound(a) To UBound(a)
MsgBox a(i), , "i=" & i
Next i
End Sub

mikerickson
06-17-2011, 06:41 AM
You could use something like
Call myRoutine(Array(singleValue))

prabhafriend
06-17-2011, 07:24 AM
You could use something like
Call myRoutine(Array(singleValue))

Not working. "Array or Userdefined Type Expected" This is the error message, highlighting the word "Array"

mikerickson
06-17-2011, 07:57 PM
If you post the routine that you are passing the arrays to, it would help.