Condition Formatting of UserForm textbox

kidwispa

Active Member
Joined
Mar 7, 2011
Messages
330
Hi All,

Following on from help in a couple of previous threads I've created a userform with a number of textboxes that are populated when a value is chosen from a combobox and a command button is clicked. In the totals section of this userform, the textbox colour changes depending on the value shown. The issue that I'm having is with one textbox where the value is zero - when it is zero I want it show as green, when "" or "-" I want it to show as grey, and greater than 0 as red. With help on here, I have been using the following code for the textbox in question:

Code:
TextBox162.Text = Worksheets("QtrData").Range("T22").Text
If TextBox162.Text <> "-" Then
If TextBox162.Text <> "" Then
If TextBox162.Text = "0" Then TextBox162.BackColor = RGB(0, 255, 0)
If TextBox162.Text = "0" Then TextBox162.ForeColor = RGB(0, 0, 0)
If TextBox162.Text > "0" Then TextBox162.BackColor = RGB(255, 0, 0)
If TextBox162.Text > "0" Then TextBox162.ForeColor = RGB(0, 0, 0)
End If
End If
If TextBox162.Text = "-" Or TextBox162.Text = "" Then TextBox162.BackColor = RGB(128, 128, 128)
If TextBox162.Text = "-" Or TextBox162.Text = "" Then TextBox162.ForeColor = RGB(0, 0, 0)

This particular textbox only shows red when there is a 0 (or any other number) and grey for "" or "-", but no green. It is confusing me as another textbox on the userform is using exactly the same code and is working correctly - the only difference is where the information is pulled from??

Can anyone help?
:)
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Code:
Sub ColorTextBox()
    Select Case Worksheets("QtrData").Range("T22").Text
        Case "", "-"
            TextBox162.BackColor = RGB(128, 128, 128)
            TextBox162.ForeColor = RGB(0, 0, 0)
        Case "0":
            TextBox162.BackColor = RGB(0, 255, 0)
            TextBox162.ForeColor = RGB(0, 0, 0)
    End Select
End Sub
 
Upvote 0
To check for 0, try removing the quotes from 0 and use Val.
Code:
If Val(Textbox162.Value) =0 Then

etc
PS Keep the quotes for the text comparisons like "-".
 
Upvote 0
Your code worked for me when the source cell was formatted as General. How is you source cell formatted?


hi Andrew,

The source cell was formatted as Number - when I changed to general it works a treat!!! Why is this?

Thanks to Norie and Sektor for your suggestions as well

:)
 
Upvote 0
Your code also worked for me when the cell was formatted as number with zero decimal places, ie 0. What was your exact format?
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

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