Creating labels in userforms

in_d

New Member
Joined
Mar 3, 2009
Messages
47
Hello,

I have a userform within a spreadsheet which, when loaded, uses vba to create a label (code below). I would like to add some code to this newly formed label, so that when it is clicked, the backcolor of the label changes to black i.e. RGB(0,0,0).

Code to create the label within the form (when the form is loaded):

Code:
Private Sub UserForm_Activate()
Dim newlabel As MSForms.Label

Set newlabel = Me.Controls.Add("Forms.label.1")
    With newlabel
        .Name = "lbl_1"
        .Top = 20
        .Left = 12
        .Height = 30
        .Width = 30
        .Caption = "Click to change"
    End With   
End Sub

Under this code in the vba editor, i added the following code, which i thought would change th backcolor of the label when clicked:

Code:
Sub lbl_1_Click()
lbl_1.Click = RGB(0, 0, 0)
End Sub

If i draw the label using the control toolbox and ignore the userform_activate code to create a new label, the form will behave as i would like.

Please can i ask your expert opinions on how you would approach this?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
thanks for the suggestion, but unfortunately this solution will not work for me.

when the label is clicked, it will doesn't change color.

i have tried to use

Code:
Set Lbl4 = Me.Controls.Add("Forms.Label.1", "lbl1")

which allows me to add a new label and use a click event, but my problem is that i can have any number up to 20 labels added using a for i = 1 to 20 loop.

i think this is tricker than i thought it would be....
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

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