VBA to change label text colour (blinking)

AllanSull

Board Regular
Joined
Oct 21, 2013
Messages
105
I've searched for something to do this but haven't managed to find anything. I'll be displaying a userform on a TV in the office and one of the labels will contain a "days to go" value. When this value falls say below 7, I want the text to change to red then back to its original colour, is there a snippet of code that can do this?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Add a new public sub to your form like this:

Code:
Public Sub CheckLabel()

If CLng(Me.Label1.Caption) < 7 Then
    Me.Label1.ForeColor = IIf(Me.Label1.ForeColor = vbRed, vbBlack, vbRed)
Else
    Me.Label1.ForeColor = vbBlack
End If

Application.OnTime Now + TimeSerial(0, 0, 2), "ChangeLabelColour"

End Sub

Change "Label1" to be your label. In UserForm_Initialize and UserForm_Terminate, add a couple of lines (if you already have these functions) or add the entire function as below:

Code:
Private Sub UserForm_Initialize()

Application.OnTime Now + TimeSerial(0, 0, 2), "ChangeLabelColour"

End Sub
Private Sub UserForm_Terminate()

Application.OnTime Now + TimeSerial(0, 0, 2), "ChangeLabelColour", Schedule:=False

End Sub

Now insert a new module into your project (or add a new method to an existing module) like this:

Code:
Public Sub ChangeLabelColour()

UserForm1.CheckLabel

End Sub

Again, change "UserForm1" to be the name of your form.

All this will create code that checks the value of the label every two seconds. If it's less than 7 then it will alternate the label colour between black and red. If it's greater than or equal to 7 then it will just display in black.

Hope that helps,

WBD
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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