View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
crazybass2 crazybass2 is offline
external usenet poster
 
Posts: 167
Default Click events not working when added during runtime

I've added a label (MyLabel) to the userform during runtime. I also used
C. Pearsons' code found here (http://www.cpearson.com/excel/vbe.htm) to add
code for a _Click Event for that label. Both the label and the code are
added correctly, but the _Click Event does not trigger.

Any thoughts? Solutions?

Mike

Private Sub MyCombo_Change()
Dim combolist
If MyCombo.ListCount = 0 Then Exit Sub
topval = 46
lb = 1
For Each wks In Worksheets
If wks.Range("A1") = MyCombo.Value Then
Set MyLabel = Controls.Add("Forms.label.1", "MyLabel" & lb)
MyLabel.Top = topval
MyLabel.Caption = wks.CodeName
AddProcedure
lb = lb + 1
topval = topval + 14
End If
Next wks
End Sub

Sub AddProcedure()
Dim VBCodeMod As CodeModule
Dim LineNum As Long
Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("Userform1").C odeModule
With VBCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Sub " & MyLabel.Name & "_Click()" & Chr(13) & _
MyLabel.Caption & ".select " & Chr(13) & _
"End Sub"
End With
End Sub

NOTE: I've tried using wks.Name with "Sheet(" & MyLabel.Caption &
").select" in as well as the wks.CodeName shown above.