according to How to address generated ListBoxes and add Items dynamically in VBA? I generated several ListBoxes dynamically which I assigned a OnAction Sub to. The OnAction Event shall show me the amount of selected Items of the current ListBox.
For better understanding:
e.g. I generated 5 Listboxes (ListBox1, ListBox2, ...) on my Worksheet "FS".
When I click 1 item on ListBox1, MsgBox shows up with "1".
If I click an additional item on ListBox1, MsgBox shows "2".
But If i click another Item of ListBox3, MsgBox shall show "1".
'ListBoxName as a variable for current ListBox and probably the most problematic line
ListBoxName = ActiveControl.Name
selectedItems = 0
Set lb = FS.ListBoxes(ListBoxName)
'The following counts the selected items in target ListBox
For i = 1 To lb.ListCount Step 1
If lb.Selected(i) Then
selectedItems = selectedItems + 1
End If
Next i
'This part puts up the Message Box with the number of selected items in target ListBox
If selectedItems > 0 Then
MsgBox selectedItems
End If
Additional information: I left out the declarations of the variables for better overview. I am not using ActiveX and I am not using a UserForm. Getting the current name of the Listbox will help me with further programming and tasks I want to do. In my mind it looked so easy to find the name of the ListBox on which I did my latest click...
Thank you in advance!
EDIT:
The code is placed in a module "Module1" in Sub Module1. In another Module "generation", where I generate the ListBoxes, the Module1 is started with lb.OnAction = "Module1.Module1"
.
EDIT2 Changed "OnClick" to "OnAction"
ActiveControl
is right. Forms listboxes don't have a Click event. – SJR