Loop through multiple listbox to save values to an array

WaqasTariq

Board Regular
Joined
Jun 26, 2012
Messages
54
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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

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