Userform Textbox conditional formatting

rmak85

New Member
Joined
Oct 3, 2018
Messages
12
Hello,
Im trying to get the text box to go red if the value is over 318 and green if under.
This code works if I put the value directly into the code.
Code:
Private Sub TextBox24_Change()
If Textbox3.Text = "Cathode Mother Roll" And TextBox24.Value > "318" Then TextBox24.BackColor = RGB(255, 0, 0) 'Red
If Textbox3.Text = "Cathode Mother Roll" And TextBox24.Value <= "318" Then TextBox24.BackColor = RGB(0, 255, 0) 'Green

If Textbox3.Text = "Cathode Mother Roll" And TextBox24.Text = "-" Then TextBox24.BackColor = RGB(255, 255, 255) 'White

End Sub


But I want the value to be based of the value in a cell in another worksheet. But the following code does not work and changes the textbox to red whatever the number i put in the box


Code:
Private Sub TextBox24_Change()

If Textbox3.Text = "Cathode Mother Roll" And TextBox24.Value > [Specs!F28].value Then TextBox24.BackColor = RGB(255, 0, 0) 'Red
If Textbox3.Text = "Cathode Mother Roll" And TextBox24.Value <= [specs!F28].value Then TextBox24.BackColor = RGB(0, 255, 0) 'Green

If Textbox3.Text = "Cathode Mother Roll" And TextBox24.Text = "-" Then TextBox24.BackColor = RGB(255, 255, 255) 'White

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
The values in a textbox are text, if you want to compare them with numerical values you need to convert them.
Code:
Private Sub TextBox24_Change()

    If Textbox3.Text = "Cathode Mother Roll" And Val(TextBox24.Value) > [Specs!F28].value Then TextBox24.BackColor = RGB(255, 0, 0) 'Red

    If Textbox3.Text = "Cathode Mother Roll" And Val(TextBox24.Value) <= [specs!F28].value Then TextBox24.BackColor = RGB(0, 255, 0) 'Green

    If Textbox3.Text = "Cathode Mother Roll" And TextBox24.Value = "-" Then TextBox24.BackColor = RGB(255, 255, 255) 'White

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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