Conditionally coloring textbox - Excel VBA

CakzPrimz

Board Regular
Joined
Oct 6, 2017
Messages
57
Dear MrExcel,

I need to coloring textbox based on its value. I've tried to write the code below, but it wont works.
If value = 0% then backcolor is Red
If value > 0 and <= 50% then backcolor is Yellow
Else backcolor = Green

Can somebody help me?

Code:
Private Sub Train3_Progress_Change()
    Train3_Progress.Value = Format(Train3_Progress.Value, "#0%")
    On Error Resume Next
    Select Case True
    Case Train3_Progress.Value = 0
        Train3_Progress.BackColor = vbRed
    Case Train3_Progress.Value >= 0.1 And Train3_Progress.Value <= 0.5
        Train3_Progress.BackColor = vbYellow
    Case Train3_Progress.Value > 0.5
        Train3_Progress.BackColor = vbGreen
    End Select
End Sub

Thank you
prima
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
One of the issues with your code is that you are converting the text box's value to a String with the Format() function, so it is no longer a numerical value.
See if this does what you want.
Code:
Dim bDisableEvent As Boolean

Private Sub Train3_Progress_Change()
  Dim tbVal As Variant

  If Not bDisableEvent Then
    tbVal = Train3_Progress.Value
    If IsNumeric(tbVal) Then
      On Error Resume Next
      Select Case tbVal
        Case 0
          Train3_Progress.BackColor = vbRed
        Case 0.1 To 0.5
          Train3_Progress.BackColor = vbYellow
        Case Is > 0.5
          Train3_Progress.BackColor = vbGreen
        Case Else
          Train3_Progress.BackColor = xlNone
      End Select
      bDisableEvent = True
      Train3_Progress.Value = Format(tbVal, "#0%")
      bDisableEvent = False
    Else
      Train3_Progress.BackColor = vbWhite
    End If
  End If
End Sub
 
Upvote 0
Dear Peter_SSs,

Glad to inform you that your code works like a charm.
It's a miracle.

Thanks so much, I really appreciate it.
prima
 
Upvote 0
You are very welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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