Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Click events not working when added during runtime

It didn't work for me, either.

But personally, I wouldn't use labels for this--or the codenames, either.

I put a combobox, a listbox, and a commandbutton on a userform.

This was the code I used:

Option Explicit
Dim BlkProc As Boolean
Private Sub MyCombo_Change()

Dim wks As Worksheet

BlkProc = True
Me.ListBox1.Clear
BlkProc = False

If Me.myCombo.ListIndex = -1 Then
Exit Sub
End If

For Each wks In Worksheets
If wks.Range("A1") = Me.myCombo.Value Then
Me.ListBox1.AddItem wks.Name
End If
Next wks
End Sub
Private Sub ListBox1_Change()

If BlkProc = True Then
Exit Sub
End If

With Me.ListBox1
If .ListIndex < 0 Then
Beep
Else
Worksheets(.List(.ListIndex)).Select
End If
End With

End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.myCombo
.AddItem "ASDF"
.AddItem "QWER"
.AddItem "ZXCV"
End With
With Me.myCombo
.Style = fmStyleDropDownList
End With
With Me.ListBox1
.Clear
.ListStyle = fmListStyleOption
.MultiSelect = fmMultiSelectSingle
End With
End Sub


I think it made the coding much easier.

crazybass2 wrote:

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.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Click events not working when added during runtime

Dave,

Thanks for the response. I tried adding commandbuttons instead of labels,
and the results were the same. The Click event added during runtime does not
work. That is the problem I need solved.

Mike

"Dave Peterson" wrote:

It didn't work for me, either.

But personally, I wouldn't use labels for this--or the codenames, either.

I put a combobox, a listbox, and a commandbutton on a userform.

This was the code I used:

Option Explicit
Dim BlkProc As Boolean
Private Sub MyCombo_Change()

Dim wks As Worksheet

BlkProc = True
Me.ListBox1.Clear
BlkProc = False

If Me.myCombo.ListIndex = -1 Then
Exit Sub
End If

For Each wks In Worksheets
If wks.Range("A1") = Me.myCombo.Value Then
Me.ListBox1.AddItem wks.Name
End If
Next wks
End Sub
Private Sub ListBox1_Change()

If BlkProc = True Then
Exit Sub
End If

With Me.ListBox1
If .ListIndex < 0 Then
Beep
Else
Worksheets(.List(.ListIndex)).Select
End If
End With

End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.myCombo
.AddItem "ASDF"
.AddItem "QWER"
.AddItem "ZXCV"
End With
With Me.myCombo
.Style = fmStyleDropDownList
End With
With Me.ListBox1
.Clear
.ListStyle = fmListStyleOption
.MultiSelect = fmMultiSelectSingle
End With
End Sub


I think it made the coding much easier.

crazybass2 wrote:

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.


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Click events not working when added during runtime

Good luck.

crazybass2 wrote:

Dave,

Thanks for the response. I tried adding commandbuttons instead of labels,
and the results were the same. The Click event added during runtime does not
work. That is the problem I need solved.

Mike

"Dave Peterson" wrote:

It didn't work for me, either.

But personally, I wouldn't use labels for this--or the codenames, either.

I put a combobox, a listbox, and a commandbutton on a userform.

This was the code I used:

Option Explicit
Dim BlkProc As Boolean
Private Sub MyCombo_Change()

Dim wks As Worksheet

BlkProc = True
Me.ListBox1.Clear
BlkProc = False

If Me.myCombo.ListIndex = -1 Then
Exit Sub
End If

For Each wks In Worksheets
If wks.Range("A1") = Me.myCombo.Value Then
Me.ListBox1.AddItem wks.Name
End If
Next wks
End Sub
Private Sub ListBox1_Change()

If BlkProc = True Then
Exit Sub
End If

With Me.ListBox1
If .ListIndex < 0 Then
Beep
Else
Worksheets(.List(.ListIndex)).Select
End If
End With

End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.myCombo
.AddItem "ASDF"
.AddItem "QWER"
.AddItem "ZXCV"
End With
With Me.myCombo
.Style = fmStyleDropDownList
End With
With Me.ListBox1
.Clear
.ListStyle = fmListStyleOption
.MultiSelect = fmMultiSelectSingle
End With
End Sub


I think it made the coding much easier.

crazybass2 wrote:

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.


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning events to runtime-created controls - is it possible? BizMark Excel Discussion (Misc queries) 1 November 20th 06 09:36 AM
Manipulate Controls added at runtime [email protected] Excel Programming 5 January 17th 06 09:50 AM
MatchEntry for combobox added during runtime Claus[_3_] Excel Programming 6 October 13th 05 12:43 PM
add event to controls added in runtime Brotha lee Excel Programming 1 May 21st 05 10:32 AM
Add Controls With Events at Runtime llowwelll Excel Programming 10 May 24th 04 11:52 AM


All times are GMT +1. The time now is 02:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"