Change Textbox Colour based on value

m0atz

Board Regular
Joined
Jul 17, 2008
Messages
247
Hi all,

I have a userform with lots of different textboxes each in groups. Just wondering if I can loop through the textboxes to change their colour based on certain values,

for example, if the value in the text box is less than 1000 then red, less than 500 then amber or less than 250 then green?

I tried the following:

Code:
Dim h
 
    For h = 5 To 17
        With UserForm1.Controls("Textbox" & h)
            If .Value < 1000 Then
                .Interior.ColorIndex = 3
            End If
        End With
    Next h

but that doesnt seem to do anything... any ideas?
cheers

colin
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Fowmy

Well-known Member
Joined
Jul 10, 2008
Messages
512
Office Version
  1. 365
Platform
  1. Windows
Add a commanbutton and try this:

Code:
Private Sub CommandButton1_Click()
For x = 1 To 5
Select Case Me.Controls("TextBox" & x).Value
    
    Case 0 To 249: Me.Controls("TextBox" & x).BackColor = vbGreen
    Case 250 To 499: Me.Controls("TextBox" & x).BackColor = vbRed
    Case 500 To 999: Me.Controls("TextBox" & x).BackColor = vbBlue

End Select
Next
End Sub
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,351
Office Version
  1. 365
Platform
  1. Windows
colin

You might want to convert what's in the textbox to a number if you want to compare it to a numeric value.

Perhaps more importantly you might want to use another property to change the colour, Interior.ColorIndex is for cells.

Oh, and the numbers used for colours are a bit different.:)
Code:
  Dim h
 
    For h = 5 To 14

        With UserForm1.Controls("Textbox" & h)

            If Val(.Value) < 1000 Then
                ' this is for font color, use BackColor for the interior color
                .ForeColor = &HC0&
            End If

        End With

    Next h

End Sub
 

m0atz

Board Regular
Joined
Jul 17, 2008
Messages
247
Thanks both,

Norie, appreciate the info, the code works great.

The values in the TB are numeric, so .value works fine. Is there a list of 'codes' for different colours?

cheers
 

Forum statistics

Threads
1,171,430
Messages
5,875,497
Members
433,131
Latest member
ThatOneDude

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
Top