Automatically change font color in text box depending on value

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
148
Office Version
  1. 365
Platform
  1. Windows
Hi. I have a small and probably simple problem. I am using text boxes linked to cells with numbers in them. The numbers change frequently and I want to have the font color change as the numbers change. Red for negative number, green for positive and black if zero. Can someone help me with this? I did some reading and found a similar question from a few years ago posted here but the solution didn't work for me. Thank you in advance.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You could use conditional formatting.
 
Upvote 0
Are you TextBoxes located on a UserForm or directly on a worksheet? If on a worksheet, is it an ActiveX TextBox?
 
Upvote 0
Hi Rick - Thank you for taking a look at this. I inserted the normal text boxes directly into the worksheet and then linked it to particular cells in the same worksheet. I did not use the ActiveX text box but am open to ideas if it helps solve the problem in an easier manner.
 
Upvote 0
With ActiveX text boxes you could use VBA. If you are not familiar with VBA: Ribbon > Developer tab > press Design button, and right click on the first text box. On the context pop-up choose Display Program Code. The VBA editor opens and in the pane at the right hand side the name of an "empty" subroutine appears. Paste the following code in the Change event subroutine of each text box as desired.
The ColorChange sub is called by the Change event routine of the/each text box. Paste at the top of the code module the four lines with the Option statement and the declarations of the three different colors.
VBA Code:
Option Explicit
Private Const cBlack    As Long = &H80000008
Private Const cRed      As Long = &HFF&
Private Const cGreen    As Long = &H8000&


Private Sub TextBox8_Change()
    '             /|\
    On Error Resume Next        '      \|/
    Call ColorChange(Me.Range(Me.TextBox8.LinkedCell))
    On Error GoTo 0
End Sub


Private Sub ColorChange(ByRef argCell As Range)
    With argCell
        If IsNumeric(.Value) Then
            If .Value > 0 Then
                .Font.Color = cGreen
            ElseIf .Value < 0 Then
                .Font.Color = cRed
            Else
                .Font.Color = cBlack
            End If
        Else
            .Font.Color = cBlack
        End If
    End With
End Sub
 
Upvote 0
You are welcome and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,258
Members
449,307
Latest member
Andile

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