VBA code for Text Box Negative Value

reginazhao

New Member
Joined
Aug 28, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I create a couple of text boxes in several graph. The text box like "(4%) sales plan", the number (4%) refers to another cell. How to write VBA to make the negative number as Red front text, and positive number as Black front text.
I tried this:

Private Sub TextBox1_Change()
If TextBox1.Value < "0%" Then
TextBox1.ForeColor = vbRed
End If
End Sub

but not working. TIA
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Assuming you are using an ActiveX, probably:
VBA Code:
Private Sub TextBox1_Change()
If IsNumeric(TextBox1.Text) Then
    If CDbl(TextBox1.Text) < 0 Then
        TextBox1.ForeColor = RGB(255, 0, 0)
    Else
        TextBox1.ForeColor = 0
    End If
Else
    TextBox1.ForeColor = 0
End If
End Sub
Bye
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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