Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 373
Default Userform Label Click

I have 30 labels on my userform. For each label that's clicked, I
want to remember the name of the label and then run a subroutine. All
of them run the same sub, and that sub does different things depending
on the name of the label. Do I have to have to have 30 different
click event subs for the 30 different labels?
TIA, James

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,117
Default Userform Label Click

if you want each click to do something different, then yes.

if you want them to do all the basic same thing, then yes, but smaller
- you could have it coded like this:

sub label1_click()
myVariable = label1.caption
call Heres_The_Real_Work
end sub

sub label2_click()
myVariable = label2.caption
call Heres_The_Real_Work
end sub

sub label3_click()
myVariable = label3.caption
call Heres_The_Real_Work
end sub


then have one sub

sub Heres_The_Real_Work()
'whatever you want it to do each time a label is clicked
end sub

hth
susan


On Jun 25, 12:03 pm, Zone wrote:
I have 30 labels on my userform. For each label that's clicked, I
want to remember the name of the label and then run a subroutine. All
of them run the same sub, and that sub does different things depending
on the name of the label. Do I have to have to have 30 different
click event subs for the 30 different labels?
TIA, James



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 373
Default Userform Label Click

Thanks, Susan. I suspected that would be the case. I already coded
it, very similar to your example. But I thought maybe there was a
simpler approach. Regards, James

On Jun 25, 12:12?pm, Susan wrote:
if you want each click to do something different, then yes.

if you want them to do all the basic same thing, then yes, but smaller
- you could have it coded like this:

sub label1_click()
myVariable = label1.caption
call Heres_The_Real_Work
end sub

sub label2_click()
myVariable = label2.caption
call Heres_The_Real_Work
end sub

sub label3_click()
myVariable = label3.caption
call Heres_The_Real_Work
end sub

then have one sub

sub Heres_The_Real_Work()
'whatever you want it to do each time a label is clicked
end sub

hth
susan

On Jun 25, 12:03 pm, Zone wrote:



I have 30 labels on my userform. For each label that's clicked, I
want to remember the name of the label and then run a subroutine. All
of them run the same sub, and that sub does different things depending
on the name of the label. Do I have to have to have 30 different
click event subs for the 30 different labels?
TIA, James- Hide quoted text -


- Show quoted text -



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Userform Label Click


You can do this very easily with a Class and a Collection. Insert a new
Class Module (from the Insert menu in the VBA editor) and give it a name of
"CLabelClass". Paste the following code in the class module:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''
Option Explicit

Public WithEvents FrmLabel As MSForms.Label

Private Sub FrmLabel_Click()
MsgBox "You clicked Label with caption: " & FrmLabel.Caption
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''

Then in your user form, insert the following code:

Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''
Private Coll As New Collection

Private Sub UserForm_Initialize()
Dim Ctrl As MSForms.Control
Dim CLabel As CLabelClass

For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.Label Then
Set CLabel = New CLabelClass
Set CLabel.FrmLabel = Ctrl
Coll.Add CLabel
End If
Next Ctrl
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''

Now when your user form starts up, it will create a new instance of
CLabelClass for each label control on your form and set that instance's
FrmLabel object to the Label control. When the user clicks on a label on the
form, the _Click event of the appropriate instance will be triggered. Put
your code in that Click event. Thus, you'll only write code once, but N
"copies" of the code, one for each Label, will exist in memory.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)



"Zone" wrote in message
oups.com...
Thanks, Susan. I suspected that would be the case. I already coded
it, very similar to your example. But I thought maybe there was a
simpler approach. Regards, James

On Jun 25, 12:12?pm, Susan wrote:
if you want each click to do something different, then yes.

if you want them to do all the basic same thing, then yes, but smaller
- you could have it coded like this:

sub label1_click()
myVariable = label1.caption
call Heres_The_Real_Work
end sub

sub label2_click()
myVariable = label2.caption
call Heres_The_Real_Work
end sub

sub label3_click()
myVariable = label3.caption
call Heres_The_Real_Work
end sub

then have one sub

sub Heres_The_Real_Work()
'whatever you want it to do each time a label is clicked
end sub

hth
susan

On Jun 25, 12:03 pm, Zone wrote:



I have 30 labels on my userform. For each label that's clicked, I
want to remember the name of the label and then run a subroutine. All
of them run the same sub, and that sub does different things depending
on the name of the label. Do I have to have to have 30 different
click event subs for the 30 different labels?
TIA, James- Hide quoted text -


- Show quoted text -




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default Userform Label Click

See below site of John titled
Handle Multiple UserForm Buttons With One Subroutine

http://www.j-walk.com/ss/excel/tips/tip44.htm

Regards,
Shailesh Shah
http://in.geocities.com/shahshaileshs/
(Excel Add-ins Page)
If You Can't Excel with Talent, Triumph with Effort.


"Zone" wrote in message
oups.com...
I have 30 labels on my userform. For each label that's clicked, I
want to remember the name of the label and then run a subroutine. All
of them run the same sub, and that sub does different things depending
on the name of the label. Do I have to have to have 30 different
click event subs for the 30 different labels?
TIA, James





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,117
Default Userform Label Click

thanks, Chip, i didn't know about using a class module.......
:)
susan


On Jun 25, 12:39 pm, "Chip Pearson" wrote:
You can do this very easily with a Class and a Collection. Insert a new
Class Module (from the Insert menu in the VBA editor) and give it a name of
"CLabelClass". Paste the following code in the class module:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''
Option Explicit

Public WithEvents FrmLabel As MSForms.Label

Private Sub FrmLabel_Click()
MsgBox "You clicked Label with caption: " & FrmLabel.Caption
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''

Then in your user form, insert the following code:

Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''
Private Coll As New Collection

Private Sub UserForm_Initialize()
Dim Ctrl As MSForms.Control
Dim CLabel As CLabelClass

For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.Label Then
Set CLabel = New CLabelClass
Set CLabel.FrmLabel = Ctrl
Coll.Add CLabel
End If
Next Ctrl
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''

Now when your user form starts up, it will create a new instance of
CLabelClass for each label control on your form and set that instance's
FrmLabel object to the Label control. When the user clicks on a label on the
form, the _Click event of the appropriate instance will be triggered. Put
your code in that Click event. Thus, you'll only write code once, but N
"copies" of the code, one for each Label, will exist in memory.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consultingwww.cpearson.com
(email on the web site)

"Zone" wrote in message

oups.com...



Thanks, Susan. I suspected that would be the case. I already coded
it, very similar to your example. But I thought maybe there was a
simpler approach. Regards, James


On Jun 25, 12:12?pm, Susan wrote:
if you want each click to do something different, then yes.


if you want them to do all the basic same thing, then yes, but smaller
- you could have it coded like this:


sub label1_click()
myVariable = label1.caption
call Heres_The_Real_Work
end sub


sub label2_click()
myVariable = label2.caption
call Heres_The_Real_Work
end sub


sub label3_click()
myVariable = label3.caption
call Heres_The_Real_Work
end sub


then have one sub


sub Heres_The_Real_Work()
'whatever you want it to do each time a label is clicked
end sub


hth
susan


On Jun 25, 12:03 pm, Zone wrote:


I have 30 labels on my userform. For each label that's clicked, I
want to remember the name of the label and then run a subroutine. All
of them run the same sub, and that sub does different things depending
on the name of the label. Do I have to have to have 30 different
click event subs for the 30 different labels?
TIA, James- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -



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
Userform Label Steve[_9_] Excel Discussion (Misc queries) 7 October 29th 07 09:51 PM
userform label double-click goes to click event John Paul Fullerton Excel Programming 3 May 19th 06 05:54 PM
Flashing UserForm Label grahammal[_25_] Excel Programming 1 March 27th 06 03:20 PM
Userform blinking label Ed Excel Programming 2 July 17th 04 12:24 AM
UserForm label doesn't load? Ed[_18_] Excel Programming 4 June 21st 04 07:53 PM


All times are GMT +1. The time now is 01:23 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"