Loop through multiple listbox to save values to an array

WaqasTariq

Board Regular
Joined
Jun 26, 2012
Messages
58
Office Version
  1. 365
I have 6 listbox'es that are named "lb_RU_#" where # is the number of listbox. each listbox has 6 options. I want to find which items are selected in each listbox and save the value to an array.

This is how I can do one list box at a time:
VBA Code:
Public i_SelectionCount1 As Integer
Public l_ForLoop1 As Long
Public s_Array_ReactorUnits() As String

ReDim s_Array_ReactorUnits(1 To 6, 1 To 6)

    i_SelectionCount1 = 0
    For l_ForLoop1 = 0 To WkS_Config.lb_RU_1.ListCount - 1
        If WkS_Config.lb_RU_1.Selected(l_ForLoop1) = True Then
            i_SelectionCount1 = i_SelectionCount1 + 1
            s_Array_ReactorUnits(1, i_SelectionCount1) = WkS_Config.lb_RU_1.List(l_ForLoop1)
        End If
    Next

How can I use two loops and be able to save the values for all 6 list boxes. I tried creating "Public lb_Test1 As ListBox" but I am not sure how to save to lb_Test1.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You didn't mention whether ListBox is Forms or ActiveX control.

Forms:
VBA Code:
Sub IterateOverListBoxesForms()
    Dim x%, lb As ListBox
    For x = 1 To 6
        Set lb = ActiveSheet.ListBoxes("lb_RU_" & x)
        '// Do something with list box
    Next
End Sub

ActiveX:

VBA Code:
Sub IterateOverListBoxesActiveX()
    Dim x%, lb As Object '//MSForms.ListBox
    For x = 1 To 6
        Set lb = ActiveSheet.OLEObjects("lb_RU_" & x).Object
        '// Do something with list box
    Next
End Sub
 
Upvote 0
ActiveX:
VBA Code:
Sub IterateOverListBoxesActiveX()
    Dim x%, lb As Object '//MSForms.ListBox
    For x = 1 To 6
        Set lb = ActiveSheet.OLEObjects("lb_RU_" & x).Object
        '// Do something with list box
    Next
End Sub

Thank you.

So I keep getting "Method or data member not found" error. Can you please take a look?

Link to the Excel file: IFD.xlsm
 
Upvote 0
Remove WkS_Config before ob_Test1, because ob_Test1 is not a member of WkS_Config worksheet.
 
Upvote 0
Solution
You're welcome!
hi.gif
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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