Consulting

Results 1 to 5 of 5

Thread: How to pass a normal datatype argument to an array parameter?

  1. #1

    How to pass a normal datatype argument to an array parameter?

    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?

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.
    [vba]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[/vba]

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You could use something like
    [vba]Call myRoutine(Array(singleValue))[/vba]

  4. #4

    Please confirm before you answer

    Quote Originally Posted by mikerickson
    You could use something like
    [vba]Call myRoutine(Array(singleValue))[/vba]
    Not working. "Array or Userdefined Type Expected" This is the error message, highlighting the word "Array"

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If you post the routine that you are passing the arrays to, it would help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •