Conditionally formatting a percentage

KenC53

New Member
Joined
Jul 24, 2011
Messages
14
I have a TextBox that references a cell on another sheet. I am formatting the textbox as a percentage and want to apply a conditional format to the textbox. The code returns an error when it gets to the section that conditionally formats the color.

Private Sub TextBox2_Change()

Dim sTemp As String, val As Single
sTemp = TextBox2.Text
If Not (InStr(1, sTemp, "%", vbTextCompare) < 1 _
And IsNumeric(sTemp)) Then _
Exit Sub '<<<<<
val = CSng(sTemp)
TextBox2.Text = Format(IIf(val < 1, val, val / 100), "0%")

If TextBox2.Text <= 0.12 Then TextBox2.BackColor = RGB(117, 146, 60)
If TextBox2.Text <= 0.12 Then TextBox2.ForeColor = RGB(0, 0, 0)
If TextBox2.Text > 0.12 Then TextBox2.BackColor = RGB(222, 42, 0)
If TextBox2.Text > 0.12 Then TextBox2.ForeColor = RGB(0, 0, 0)
If TextBox2.Text = "-" Or TextBox2.Text = "" Then TextBox2.BackColor = RGB(128, 128, 128)
If TextBox2.Text = "-" Or TextBox2.Text = "" Then TextBox2.ForeColor = RGB(0, 0, 0)
End Sub


The error occurs at the first If TextBox2.Text <= 0.12 statement. If i disable the section that applies the %, the conditional formatting works correctly but then the number is not a percentage.

Any suggestions
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
The problem is that you are trying to test a text string against a numeric value. The easiest solution would be to create another variable, or redefine val before formatting it as text, then use that variable in your conditional formatting section.
 
Upvote 0
Forgive me I'm a newbie. How would I create the new variable then use that variable in my conditional formatting section?
 
Upvote 0
Try something like this: (add val2 to your Dim line as Single)

Change

val = CSng(sTemp)
TextBox2.Text = Format(IIf(val < 1, val, val / 100), "0%")

to

val = CSng(sTemp)
val2=IIf(val < 1, val, val / 100)
TextBox2.Text = Format(val2, "0%")


and

If TextBox2.Text <= 0.12 Then TextBox2.BackColor = RGB(117, 146, 60)
If TextBox2.Text <= 0.12 Then TextBox2.ForeColor = RGB(0, 0, 0)
If TextBox2.Text > 0.12 Then TextBox2.BackColor = RGB(222, 42, 0)
If TextBox2.Text > 0.12 Then TextBox2.ForeColor = RGB(0, 0, 0)

to

If val2 <= 0.12 Then TextBox2.BackColor = RGB(117, 146, 60)
If val2 <= 0.12 Then TextBox2.ForeColor = RGB(0, 0, 0)
If val2 > 0.12 Then TextBox2.BackColor = RGB(222, 42, 0)
If val2 > 0.12 Then TextBox2.ForeColor = RGB(0, 0, 0)
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,548
Members
452,927
Latest member
rows and columns

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