Change UserForm Label Color on click...code shared with multiple Labels

zeeunit1

New Member
Joined
Feb 7, 2019
Messages
6
Hello, and Thank You in Advance.

I have an excel user form with numerous labels (from the form tools). When you click on the label, it initiates the code below and cycles through three colors. I have used this in the past and a friend of my had simplified the coding to pull from one location ("call..." I believe), however, I can't for the life of me remember how to write this to refer each label click to the same seven-ish lines of code to save me from repeating this code 100 times. I want the click to only affect the label that was clicked, not change them all with one click.

Again, thank you.

Tim.

[Private Sub Label4_Click()
If Label4.BackColor = &H8000000F Then
Label4.BackColor = vbYellow
ElseIf Label4.BackColor = vbYellow Then
Label4.BackColor = vbRed
ElseIf Label4.BackColor = vbRed Then
Label4.BackColor = &H8000000F
End If
End Sub]
 
@MAIT
IN the VBE select Insert from the menu > Class module > in the properties window change Class1 to CLabels
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
OK Thanks. I have seen that before and never used it or looked into it. I just forgot I had seen it. Do you know how to refer to the control just cliked on?

So if I click on Label1 it can call macro named Color which sets label color.
And if I click on Label2 it calls Macro named Color

I thought it would be Me.Activecontrol

So if I had a Macro named Color

In each label I could have just enter Call Color
And it would set Label color to what script name

The basic question is how can I get the name of the control I just clicked on

If I click on Label1 I want to get answer=Label1.Name
If I click on Label2 I want to get answer=Label2.Name

These are userform Labels.

I'm not hijacking this question.
If I knew this answer I could give the original poster the answer he wants.

@MAIT
IN the VBE select Insert from the menu > Class module > in the properties window change Class1 to CLabels
 
Last edited:
Upvote 0
The only way I know of doing that is to have a separate click event for each label, as shown in post#10
I don't think there is a way of having a "generic" code, like you can with shapes on a sheet using application.caller
 
Upvote 0
You can alter the class module code I posted to get the name of the label that has been clicked, the code I posted is actually adapted from code that did exactly that.
Code:
Private Sub mLabelGroup_Click()

    MsgBox "You clicked the label " & mLabelGroup.Name & " which has the caption " & mLabelGroup.Caption

    If mLabelGroup.BackColor = &H8000000F Then
        mLabelGroup.BackColor = vbYellow
    ElseIf mLabelGroup.BackColor = vbYellow Then
        mLabelGroup.BackColor = vbRed
    ElseIf mLabelGroup.BackColor = vbRed Then
        mLabelGroup.BackColor = &H8000000F
    End If
End Sub
 
Upvote 0
Ok thanks from me to both Norie and Fluff. Hope this helps the original poster
 
Upvote 0
@Fluff - Thank You, that seems to ring a bell with the code I had and lost! It works like a charm!

@Norie - Looks like I misunderstood the part about it attaching itself to all of the labels as occurring with one click. I will be giving this a run as well.

You are all too kind!
 
Upvote 0
Norie's is the best way to go, far less code to write :)
 
Upvote 0
zeeunit1

What part did you not understand?

I can explain further if you want.
 
Upvote 0

Forum statistics

Threads
1,215,561
Messages
6,125,533
Members
449,236
Latest member
Afua

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