Event for Dynamic Label or TextBox on UserForm

vthokienj

Board Regular
Joined
Aug 1, 2011
Messages
103
I create a series of labels and text boxes at runtime.

Code:
for x = 1 to 10
        labelName = "label" & x
        Set cControlLabel = Me.frameScrollable.Controls.Add("Forms.Label.1",     labelName, True)
next x
How can I assign an on_click event to these? I have seen some rather complicated suggestions such as creating code for each possible object, but there could possibly be hundreds of labels created. It would be nice to be able to call the same macro from each label click, and then take action based on the .caption property of the label.

Thanks
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
First can I ask if you've considered using some othe control, for example a listbox which is meant to be used to list multiple items?

Anyway, if you do want to keep the labels you'll could use a class module to handle the click events of all of them.

What do you want the click event for each label to do?
 
Upvote 0
I position the labels over textboxes where data will be entered. If the label is clicked, I then want to load another userform where additional data will be collected, which would be tied to the data in the respective textbox. So a listbox would not work for me.
 
Upvote 0
How are you tying the labels and textboxes to each other?

Also, again, what exactly do you want to do when they are clicked?
 
Upvote 0
That's what I suggested - a class module.

Just wanted to know more before posting any code, or uploaded an example workbook.
 
Upvote 0
Oh right, the class module is what you suggested. I guess I just couldn't picture how it would work at first but you did provide the right solution for me. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,216,046
Messages
6,128,489
Members
449,455
Latest member
jesski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top