+ Reply to Thread
Results 1 to 6 of 6

Populate list box with multi-dimensional array

  1. #1
    Graham Whitehead
    Guest

    Populate list box with multi-dimensional array

    Hi, I was just wondering if it possible to populate a list box with an array
    that was say 8 x 8. Basically, I organised a whole bunch of data and would
    like to present it to a user inside a listbox on a form.



  2. #2
    NickHK
    Guest

    Re: Populate list box with multi-dimensional array

    Graham,
    Does this work for you:

    Private Sub CommandButton1_Click()
    Dim arr(1 To 8, 1 To 8) As String
    Dim i As Long
    Dim j As Long

    'Fill array, but can come from a Range or wherever
    For i = 1 To 8
    For j = 1 To 8
    arr(i, j) = "Item " & i & ", " & j
    Next
    Next

    With ListBox1
    .Clear
    .ColumnCount = UBound(arr, 2)
    .List = arr()
    End With

    End Sub

    NickHK

    End Sub
    "Graham Whitehead" <[email protected]> wrote in message
    news:[email protected]...
    > Hi, I was just wondering if it possible to populate a list box with an

    array
    > that was say 8 x 8. Basically, I organised a whole bunch of data and

    would
    > like to present it to a user inside a listbox on a form.
    >
    >




  3. #3
    Graham Whitehead
    Guest

    Re: Populate list box with multi-dimensional array

    Thanks for the help Nick. I see how this works except for the line:

    .ColumnCount = UBound(arr, 2)

    Where does the 2 come from and what does it do?

    "NickHK" <[email protected]> wrote in message
    news:[email protected]...
    > Graham,
    > Does this work for you:
    >
    > Private Sub CommandButton1_Click()
    > Dim arr(1 To 8, 1 To 8) As String
    > Dim i As Long
    > Dim j As Long
    >
    > 'Fill array, but can come from a Range or wherever
    > For i = 1 To 8
    > For j = 1 To 8
    > arr(i, j) = "Item " & i & ", " & j
    > Next
    > Next
    >
    > With ListBox1
    > .Clear
    > .ColumnCount = UBound(arr, 2)
    > .List = arr()
    > End With
    >
    > End Sub
    >
    > NickHK
    >
    > End Sub
    > "Graham Whitehead" <[email protected]> wrote in message
    > news:[email protected]...
    >> Hi, I was just wondering if it possible to populate a list box with an

    > array
    >> that was say 8 x 8. Basically, I organised a whole bunch of data and

    > would
    >> like to present it to a user inside a listbox on a form.
    >>
    >>

    >
    >




  4. #4
    Graham Whitehead
    Guest

    Re: Populate list box with multi-dimensional array

    I managed to get your example working, and it is nearly what I am looking
    for. However, only want one item from the array in each column. For
    example, say the array contained an 8 x 8 boolean evaluation of values - I
    would want only the result for the value in that column, i.e. [1,1] would
    just say False - becasue the arr(1,1) value is false. Hope this makes
    sense.

    BTW Am I right in thinking that the 2 refers to the number of items
    contained withing the array? (still a bit fuzzy on this one).

    Thanks

    "Graham Whitehead" <[email protected]> wrote in message
    news:[email protected]...
    > Thanks for the help Nick. I see how this works except for the line:
    >
    > .ColumnCount = UBound(arr, 2)
    >
    > Where does the 2 come from and what does it do?
    >
    > "NickHK" <[email protected]> wrote in message
    > news:[email protected]...
    >> Graham,
    >> Does this work for you:
    >>
    >> Private Sub CommandButton1_Click()
    >> Dim arr(1 To 8, 1 To 8) As String
    >> Dim i As Long
    >> Dim j As Long
    >>
    >> 'Fill array, but can come from a Range or wherever
    >> For i = 1 To 8
    >> For j = 1 To 8
    >> arr(i, j) = "Item " & i & ", " & j
    >> Next
    >> Next
    >>
    >> With ListBox1
    >> .Clear
    >> .ColumnCount = UBound(arr, 2)
    >> .List = arr()
    >> End With
    >>
    >> End Sub
    >>
    >> NickHK
    >>
    >> End Sub
    >> "Graham Whitehead" <[email protected]> wrote in message
    >> news:[email protected]...
    >>> Hi, I was just wondering if it possible to populate a list box with an

    >> array
    >>> that was say 8 x 8. Basically, I organised a whole bunch of data and

    >> would
    >>> like to present it to a user inside a listbox on a form.
    >>>
    >>>

    >>
    >>

    >
    >




  5. #5
    NickHK
    Guest

    Re: Populate list box with multi-dimensional array

    Graham,
    Read the help about L/UBound.
    As you can have multi-dimensional arrays, you need to specify which
    dimension you want info on.
    In this case it is the 2nd dimension, so the number of columns in the
    ListBox match the number of horizontal elements in the array.
    Otherwise it defaults to 1.
    Actually, it would be better as :
    ..ColumnCount = UBound(arr, 2)-LBound(arr, 2)+1
    to handle 0, 1 or whatever based array.

    NickHK

    "Graham Whitehead" <[email protected]> wrote in message
    news:[email protected]...
    > Thanks for the help Nick. I see how this works except for the line:
    >
    > .ColumnCount = UBound(arr, 2)
    >
    > Where does the 2 come from and what does it do?
    >
    > "NickHK" <[email protected]> wrote in message
    > news:[email protected]...
    > > Graham,
    > > Does this work for you:
    > >
    > > Private Sub CommandButton1_Click()
    > > Dim arr(1 To 8, 1 To 8) As String
    > > Dim i As Long
    > > Dim j As Long
    > >
    > > 'Fill array, but can come from a Range or wherever
    > > For i = 1 To 8
    > > For j = 1 To 8
    > > arr(i, j) = "Item " & i & ", " & j
    > > Next
    > > Next
    > >
    > > With ListBox1
    > > .Clear
    > > .ColumnCount = UBound(arr, 2)
    > > .List = arr()
    > > End With
    > >
    > > End Sub
    > >
    > > NickHK
    > >
    > > End Sub
    > > "Graham Whitehead" <[email protected]> wrote in message
    > > news:[email protected]...
    > >> Hi, I was just wondering if it possible to populate a list box with an

    > > array
    > >> that was say 8 x 8. Basically, I organised a whole bunch of data and

    > > would
    > >> like to present it to a user inside a listbox on a form.
    > >>
    > >>

    > >
    > >

    >
    >




  6. #6
    NickHK
    Guest

    Re: Populate list box with multi-dimensional array

    Graham,
    Unless I'm missing something, that's what you have there;
    8 columns x 8 rows with the array value displayed in the corresponding
    position in the ListBox.

    NickHK

    "Graham Whitehead" <[email protected]> wrote in message
    news:[email protected]...
    > I managed to get your example working, and it is nearly what I am looking
    > for. However, only want one item from the array in each column. For
    > example, say the array contained an 8 x 8 boolean evaluation of values - I
    > would want only the result for the value in that column, i.e. [1,1] would
    > just say False - becasue the arr(1,1) value is false. Hope this makes
    > sense.
    >
    > BTW Am I right in thinking that the 2 refers to the number of items
    > contained withing the array? (still a bit fuzzy on this one).
    >
    > Thanks
    >
    > "Graham Whitehead" <[email protected]> wrote in message
    > news:[email protected]...
    > > Thanks for the help Nick. I see how this works except for the line:
    > >
    > > .ColumnCount = UBound(arr, 2)
    > >
    > > Where does the 2 come from and what does it do?
    > >
    > > "NickHK" <[email protected]> wrote in message
    > > news:[email protected]...
    > >> Graham,
    > >> Does this work for you:
    > >>
    > >> Private Sub CommandButton1_Click()
    > >> Dim arr(1 To 8, 1 To 8) As String
    > >> Dim i As Long
    > >> Dim j As Long
    > >>
    > >> 'Fill array, but can come from a Range or wherever
    > >> For i = 1 To 8
    > >> For j = 1 To 8
    > >> arr(i, j) = "Item " & i & ", " & j
    > >> Next
    > >> Next
    > >>
    > >> With ListBox1
    > >> .Clear
    > >> .ColumnCount = UBound(arr, 2)
    > >> .List = arr()
    > >> End With
    > >>
    > >> End Sub
    > >>
    > >> NickHK
    > >>
    > >> End Sub
    > >> "Graham Whitehead" <[email protected]> wrote in message
    > >> news:[email protected]...
    > >>> Hi, I was just wondering if it possible to populate a list box with an
    > >> array
    > >>> that was say 8 x 8. Basically, I organised a whole bunch of data and
    > >> would
    > >>> like to present it to a user inside a listbox on a form.
    > >>>
    > >>>
    > >>
    > >>

    > >
    > >

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1