change text box font color based on value of a cell

frankdethier

New Member
Joined
Mar 1, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
hi

I'm a VBA newbie, but looking for a solution to change the color of a text box based on the value in another cell. In a nutshell
score < 1.5 --> color = deep red
1.5 < score < 2.5 --> color = light red
2.5 < score < 3.5 --> color = orange
3.5 < score < 4.5 --> color = light green
4.5 < score --> color = deep green
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,

Welcome to MrExcel board. Please check below code:

txtCode and txtBackColor is textfield name.

VBA Code:
Private Sub txtCode_Change()
Select Case txtCode.Value
    Case Is < 1.5
        txtBackColor.BackColor = RGB(200, 0, 0)
    Case Is < 2.5
        txtBackColor.BackColor = vbRed
    Case Is < 3.5
        txtBackColor.BackColor = RGB(255, 165, 0)
    Case Is < 4.5
        txtBackColor.BackColor = vbGreen
    Case Is > 4.5
        txtBackColor.BackColor = RGB(0, 100, 0)
    Case Else
        txtBackColor.BackColor = vbWhite
End Select
End Sub
 
Upvote 0
thanks but I'm not there yet. So nothing is done/executed yet. This runs automatically?

1. Value in text box is linked to a value in another cell ... so the value between 0 and 5 is actually ''=H33''. Does it work then? Is that seen as a value or as an equation?

2. My text box name is CCBox. So I need to replace in your code above both ''txtCode'' and ''txtBackColor'' with ''CCBox''?

Sorry for my naive questions, but it's the first time I'm experimenting with VBA code.
 
Upvote 0

Forum statistics

Threads
1,215,836
Messages
6,127,179
Members
449,368
Latest member
JayHo

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