Reading all items from a Listbox into an array

AndyGalloway

Board Regular
Joined
Apr 24, 2019
Messages
51
Hi guys. This problem is driving me bananas. I have two list boxes. The first is populated with training subjects that are outstanding for an employee. Using buttons, some subjects are moved across to the second list box. Now, when I click on the OK button, I want all items in the second list box to be copied into an array called strSubject. strSubject() is defined publicly as a string. The question is, how do I copy the items in list box 2 into the array? I won't confuse the issue by posting my failed attempts to code this.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
at top of a standard module
VBA Code:
Public strSubject() As String

You did not state which type of listboxes you are using
- code below tested with active-x listbox (code goes in sheet module) and userform listbox (code goes in userform module)
VBA Code:
    Dim i As Long
    ReDim strSubject(ListBox2.ListCount - 1)
    For i = 0 To ListBox2.ListCount - 1
        strSubject(i) = ListBox2.List(i)
        Debug.Print strSubject(i)           'see output in immediate window
    Next
 
Upvote 0
What type of list box are you using? This seems to work for Forms control listbox with single column list.
Code:
Sub t()
Dim ary As Variant
ary = Sheet1.Shapes("List Box 1").ControlFormat.List
MsgBox ary(UBound(ary))
End Sub
 
Upvote 0
at top of a standard module
VBA Code:
Public strSubject() As String

You did not state which type of listboxes you are using
- code below tested with active-x listbox (code goes in sheet module) and userform listbox (code goes in userform module)
VBA Code:
    Dim i As Long
    ReDim strSubject(ListBox2.ListCount - 1)
    For i = 0 To ListBox2.ListCount - 1
        strSubject(i) = ListBox2.List(i)
        Debug.Print strSubject(i)           'see output in immediate window
    Next
This is exactly what I wanted. I got so close to coding this myself, but I could not find reference to how to reference an item in the listbox list i.e. ListBox2(i). Thank you for your help.
 
Upvote 0
Why not just use
VBA Code:
MyArray=ListBox1.List
 
Upvote 0
Same method that JLGWhiz and Fluff have pointed out...

VBA Code:
    'transfer the contents of listbox to variant array
    Dim listboxItems As Variant
    listboxItems = ListBox1.List
    
    'print items from listbox to Immediate Window
    Dim i As Long
    For i = LBound(listboxItems) To UBound(listboxItems)
        Debug.Print listboxItems(i, 0)
    Next i
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top