VBA: change label forecolor (Integer)

Xlacs

Board Regular
Joined
Mar 31, 2021
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I'm wondering if I'm doing this correctly because my label caption forecolor remains "red" given my code below.
Hoping some could enlighten me with the below code. Thank you!!

VBA Code:
If Val(Me.lbl1.caption) < 85 Then
Me.lbl1.forecolor = RGB(255,0,0,)
Else
Me.lbl1.forecolor = RGB(0,0,0)

End If

I also tried

VBA Code:
If Val(Me.lbl1) < 85 Then
    Me.lbl1.forecolor = RGB(255,0,0,)
    Else
    Me.lbl1.forecolor = RGB(0,0,0)
    
    End If

However, the result is regardless the value, the forecolor remains red.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Labels don't have values, so 2nd example will never work.
Can you show some examples of label captions where this doesn't work as expected to make sure there's no issue there as well?
 
Last edited:
Upvote 0
Labels don't have values, so 2nd example will never work.
Can you show some examples of label captions where this doesn't work as expected to make sure there's no issue there as well?

You're right. What I mean is caption.

I don't have one, however, example of lbl1 caption is 80%. And my above code, does not recognize the statement
 
Upvote 0
Hmm, well this works for me where the caption is 80% but I tested in Access. Shouldn't matter as the vba should work the same.
VBA Code:
Function TestXlacsCode()
If Val(Me.Label176.Caption) < 85 Then
     MsgBox "Label caption is less than 85"
End If

End Function
Try this before your Val statement and check the output in the immediate window:
Debug.Print Val(Me.lbl1.Caption)

No, I don't use names like that in my code; this is a play db that somebody posted in a forum.
Love your user name!

EDIT - by any chance you turned off error handling?
 
Upvote 0
Hmm, well this works for me where the caption is 80% but I tested in Access. Shouldn't matter as the vba should work the same.
VBA Code:
Function TestXlacsCode()
If Val(Me.Label176.Caption) < 85 Then
     MsgBox "Label caption is less than 85"
End If

End Function
Try this before your Val statement and check the output in the immediate window:
Debug.Print Val(Me.lbl1.Caption)

No, I don't use names like that in my code; this is a play db that somebody posted in a forum.
Love your user name!
Will give this a try
 
Upvote 0
Tried it in Excel - same code works. Our posts probably crossed & you didn't see my edit?
Yes, do try the debug thing.
 
Upvote 0
Tried it in Excel - same code works. Our posts probably crossed & you didn't see my edit?
Yes, do try the debug thing.


hmm.. weird. but nothing is happening.. maybe Im doing this wrong
 
Upvote 0
Hmm t
You didn't say, but this label is on a userform, yes?

Asked but not answered

and

result not reported? That's because code isn't running?

Hmm NO respond.

THis is how I code it. Do you think something is wrong here that triggers it?

VBA Code:
Private Sub Userform_initialize()

Me.lbl1.caption = Format(Sheets("Sheet1").Range("D3").value, "0.00%"
Me.lbl2.caption = Format(Sheets("Sheet1").Range("D4").value, "0.00%"
.
.
.
        If Val(Me.lbl1.caption) < 85 Then
        Me.lbl1.forecolor = RGB(255,0,0,)
        Else
        Me.lbl1.forecolor = RGB(0,0,0)
    
    If Val(Me.lbl2.caption) < 85 Then
    Me.lbl2.forecolor = RGB(255,0,0,)
    Else
    Me.lbl2.forecolor = RGB(0,0,0)
    
    End If
    
    If Val(Me.lbl3.caption) < 85 Then
    Me.lbl3.forecolor = RGB(255,0,0,)
    Else
    Me.lbl3.forecolor = RGB(0,0,0)
    
    End If
    
    
        If Val(Me.lbl4.caption) < 85 Then
    Me.lbl4.forecolor = RGB(255,0,0,)
    Else
    Me.lbl4.forecolor = RGB(0,0,0)
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,258
Members
449,307
Latest member
Andile

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