changing the color of a label on a userform based on whether something is entered into a textbox...

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have similar code (which works prefectly) to change the color of a label when its based on whether a check box is ticked or not:
chkBoxColor.JPG

If its checked, it turns the label yellow (2 labels yellow, in this case), and when I uncheck it, it goes back to its original color:

VBA Code:
Private Sub chkCSR10J_Click() ' PRODUCTS ARE WRONG or OFF COLOR
If chkCSR10J = True Then Me.lblCSR10J2.BackColor = &HFFFF&
If chkCSR10J = False Then Me.lblCSR10J2.BackColor = &HFFFFC0
If chkCSR10J = True Then Me.chk23W.value = True
If chkCSR10J = False And chkCSR9I = False And chkCSR11K = False And chkCSR12L = False And chkCSR13M = False Then Me.chk23W.value = False
End Sub

But when I attempt to do something similar but with a textbox instead of a checkbox, it doesn't quite fully work (it does change the label to yellow when there is text present... but, when I delete the textbox and make it empty, it still stays yellow and it doesn't go back to the original color...(?)

with text:
txtBoxColor.png


with no text: (the label remains yellow):
txtBoxColor1.png

current code: (which is not working... it wont change the label (lblProblem1) back to gray when the textbox is blank... thanks for any help anyone can offer.
VBA Code:
Private Sub txtProblem1_Change()
If txtProblem1.value = Null Then lblProblem1.BackColor = &HE0E0E0   ' GRAY
If txtProblem1.value <> "" Then lblProblem1.BackColor = &HFFFF&     ' YELLOW
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try
VBA Code:
If txtProblem1.value = "" Then lblProblem1.BackColor = &HE0E0E0   ' GRAY
 
Upvote 0
Solution
Hi,
try

VBA Code:
Private Sub txtProblem1_Change()
 With Me.txtProblem1
    .BackColor = IIf(Len(.Value) > 0, &HFFFF&, &HE0E0E0)
 End With
End Sub

Dave
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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