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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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
 
Upvote 0
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
 
Upvote 0
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
 
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