Hi Shane,
Do all of these 300 labels need click event handlers? If yes, then I think the custom class option would be worth looking at because it means you would only need one communal event handler which can be set up at design time.
Here is an example with 10 labels (please excuse all the magic numbers for the sizing etc.. - the focus is the event handling):
In the
userform class module:
Code:
Option Explicit
Private pLabels As Collection
Private Sub UserForm_Initialize()
Dim clsLabel As c_Label
Dim lblNew As MSForms.Label
Dim i As Long
Set pLabels = New Collection
'size our userform
Width = 90
Height = 250
'add 10 labels
For i = 1 To 10
Set lblNew = Controls.Add( _
bstrProgID:="Forms.Label.1", _
Name:="Line" & i, _
Visible:=True)
'position the label etc...
With lblNew
With .Font
.Size = 7
.Name = "Comic Sans MS"
End With
.Caption = "Click On Me"
.Top = i * 20
.Height = 12
.Left = 20
.Width = 50
End With
Set clsLabel = New c_Label
Set clsLabel.lbl = lblNew
pLabels.Add clsLabel
Next i
End Sub
Then go to Insert | Class Module to insert a custom class module. In the properties window rename it to
c_Label. Then paste in the following code:
Code:
Option Explicit
Private WithEvents pLabel As MSForms.Label
Public Property Set lbl(Value As MSForms.Label)
Set pLabel = Value
End Property
Private Sub pLabel_Click()
MsgBox "You clicked on label: " & pLabel.Name
End Sub