Conditional Format a TextBox

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
185
Office Version
  1. 365
Platform
  1. Windows
I would appreciate some assistance applying condtional formatting to a textbox that contains the result of this formula

VBA Code:
If TextBox1.Value = "No" Then
        TextBox2.Value = "Not Active"
    Else
       TextBox2 = Range("T8").Text

Range("T8") [From another active WS] also contains a formula that will return a variable amount (If TRUE) or "£0.00" (If FALSE).
Values in TextBox2 will be as TEXT

I am trying to apply conditional formatting to TextBox2 when the returned value is the variable amount (i.e. the value is not either "Not Active" or "£0.00")
I have tried the following code, but this applies the formatting to any value, I assume because the actual content of TextBox2 is a formula and not a text value

VBA Code:
Private Sub TextBox2_Change()
If TextBox2 = "Not Active" Or Not "£0.00" Then
TextBox2.BackColor = RGB(0, 255, 0)
End If
End Sub

I have also tried using TextBox2.Text and TextBox2.Value
I have even tried adding the folowing but without success.

If TextBox2.= "Not Active" Then TextBox2.BackColor = RGB(0, 0, 0)
If TextBox2 = "£0.00" Then TextBox2.BackColor = RGB(0, 0, 0)

Thanks in advance
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
VBA Code:
set Textbox2 = sheet2.Shapes(1)
Textbox2.Textframe2.TextRange.Text = "Stuff"
Debug.Print Textbox2.Textframe2.TextRange.Text
 
Upvote 0
Thank you for taking the time to try to resolve my enquiry.
I have to be honest and admit that I am a relative beginner when it comes to VBA but I always try to understand the logic behind expressions, their aims the syntax required.

Unfortunately, I am totally confused by this "solution" as I cannot even start to understand what this is trying to achieve, where is should be placed and what confilcts this might have, given that I have multiple sheets in the workbook. I also need to apply this to a significant number of TextBoxes so this cannot be a generic solution.
 
Upvote 0
I have multiple sheets in the workbook. I also need to apply this to a significant number of TextBoxes so this cannot be a generic solution.

Hi,
I am assuming that you have ActiveX Textboxes in your worksheets & your CF code would be same for all?
If so, you can create a common code that each textbox can call

see if following update to your code will do what you want

Place in a STANDARD module

VBA Code:
Sub CF(ByVal objTextBox As Object)
    Dim Valid   As XlRgbColor, NotValid As XlRgbColor
    Dim m       As Variant
  
   'Valid = the value is not either "Not Active" or "£0.00"
    Valid = rgbLime
    NotValid = rgbWhite
  
    With objTextBox
        m = Application.Match(.Value, Array("Not Active", "£0.00"), 0)
        .BackColor = IIf(Len(.Value) > 0 And IsError(m), Valid, NotValid)
    End With
End Sub

For each of your worksheet TextBox_Change events, you pass the TextBox object to the code

VBA Code:
Private Sub TextBox2_Change()
    CF Me.TextBox2
End Sub

Hope I have understood your requirement correctly but amend code as required

Hope Helpful

Dave
 
Upvote 0
Solution
VBA Code:
Sub CF(ByVal objTextBox As Object)
    Dim Valid   As XlRgbColor, NotValid As XlRgbColor
    Dim m       As Variant
 
   'Valid = the value is not either "Not Active" or "£0.00"
    Valid = rgbLime
    NotValid = rgbWhite


I did not know about XlRgbColor enumeration. Thank you for that.
 
Upvote 0
Hi,
I am assuming that you have ActiveX Textboxes in your worksheets & your CF code would be same for all?
If so, you can create a common code that each textbox can call

see if following update to your code will do what you want

Place in a STANDARD module

VBA Code:
Sub CF(ByVal objTextBox As Object)
    Dim Valid   As XlRgbColor, NotValid As XlRgbColor
    Dim m       As Variant
 
   'Valid = the value is not either "Not Active" or "£0.00"
    Valid = rgbLime
    NotValid = rgbWhite
 
    With objTextBox
        m = Application.Match(.Value, Array("Not Active", "£0.00"), 0)
        .BackColor = IIf(Len(.Value) > 0 And IsError(m), Valid, NotValid)
    End With
End Sub

For each of your worksheet TextBox_Change events, you pass the TextBox object to the code

VBA Code:
Private Sub TextBox2_Change()
    CF Me.TextBox2
End Sub

Hope I have understood your requirement correctly but amend code as required

Hope Helpful

Dave
Thank You.. This works perfectly
 
Upvote 0
I did not know about XlRgbColor enumeration. Thank you for that.

Welcome - always worth a rummage in the object Browser to see what gems MS have included.

Dave
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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