Skip to main content

How to change textbox color based on value in Excel?

In Excel, we can apply the Conditional Formatting function to change the background color based on cell value, but, in this article, I will talk about how to change textbox color based on cell value or value in textbox.

Change textbox color based on cell value with VBA code

Change textbox color based on value in textbox with VBA code


arrow blue right bubble Change textbox color based on cell value with VBA code

Let’s say, if cell value in A1 is greater than cell value in B1, I want the textbox to be filled with red color, on the contrary, the textbox should be filled with yellow color. Please achieve this job with following steps:

1. Insert a textbox by clicking Developer > Insert > Text Box (ActiveX Control), and then draw a textbox, see screenshot:

doc textbox color based on value 1

2. Then right click the textbox, and select View Code from the context menu to open the Microsoft Visual Basic for Applications window, and then replace the original code with the following VBA code into the blank module:

VBA code: Change textbox color based on cell values:

Private Sub TextBox1_Change()
    If ActiveSheet.Range("A1").Value > ActiveSheet.Range("B1").Value Then
        Me.TextBox1.BackColor = vbRed
    Else
        Me.TextBox1.BackColor = vbYellow
    End If
End Sub

doc textbox color based on value 2

Note: In the above code, TextBox1 is the textbox name that you inserted, A1 and B1 are the two cells you want to change textbox color based on, please change them to your need.

3. Then save and close the code window, and exit the Design mode, now, if cell A1 is greater than B1, when you type text in the textbox, it will be filled with red color, and if A1 is less than B1, when entering value in the textbox, it will be filled with yellow color, see screenshot:

doc textbox color based on value 3


arrow blue right bubble Change textbox color based on value in textbox with VBA code

If you want to change the textbox color based on the value in textbox, for example, when value in textbox is between 1 and 10, the textbox’s filled color is red, the textbox color is green if value is between 11 and 20, if is other values, the textbox color is yellow. To deal with this task, please apply the below VBA code.

1. After inserting a textbox, right click it, and select View Code from the context menu to open the Microsoft Visual Basic for Applications window, and then replace the original code with the following VBA code into the blank module:

VBA code: Change textbox color based on value in textbox:

Private Sub TextBox1_Change()
    On Error Resume Next
    Select Case TextBox1.Value
        Case 1 To 10:
            TextBox1.BackColor = vbRed
        Case 11 To 20:
            TextBox1.BackColor = vbGreen
        Case Else:
            TextBox1.BackColor = vbYellow
    End Select
End Sub

doc textbox color based on value 4

Note: In the above code, Textbox1 is the name of the textbox you are inserted, and you can change the values and background color within the code to your own.

2. Then save and close the code window, and exit the Design Mode, now, if you enter the value between 1 and 10 into the textbox, its background color will become red, the value between 11 and 20, the textbox background color will become green, other values, it will become yellow as following screenshot shown:

doc textbox color based on value 5


Related articles:

How to insert picture into text box?

How to set a default value in a textbox?

How to allow only numbers to be input in text box?

How to apply spell check in textbox?

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (3)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Nevermind, I figured it out. Instead of making the event which triggers the change "Textbox1_GotFocus()", I made it "Worksheet_SelectionChange(ByVal Target As Range)". Now any time the value in one of the cells changes, the textbox color changes automatically.
This comment was minimized by the moderator on the site
For me, when trying to change the textbox color value based on two cell values, the textbox only updates if you type something into the textbox. I need it to update automatically. Guess I'll just have to learn VBA to figure out why.
This comment was minimized by the moderator on the site
Can someone provide an example workbook so I can see this working? I keep trying but to no avail. Thanks
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations