I’m trying to store selected values from a listbox (Excel 2010) with multi selection enabled. This is easily done by iterating through the items in the list to see if they are selected. However, upon adding a number of listboxes, i have to create a callback for each:
Sub ListBox1_Changed()
Call DoStuff(Worksheets("Sheet1").ListBoxes(1))
End Sub
Sub ListBox2_Changed()
Call DoStuff(Worksheets("Sheet1").ListBoxes(2))
End Sub
Sub DoStuff(L as ListBox)
'Do stuff here
Sub
Eventually I will end up with a large number of these ListBoxes across multiple worksheets.
Now my question is: Is it possible to reference the specific box that called the macro and assigning this single function for all my listboxes? I’m guessing something like:
Sub ListBox_Changed(ByVal L as Object)
' This will not work btw ^^^^^^^^
' Magic code goes here.
Call DoStuff(L_converted_to_ListBox_Format)
End Sub
Please note, that I’m not using userforms but have just put the listbox directly in the worksheet.
Thanks!
You can use the
Application.Callerto determine whichListBoxcalled theSub, like this