Search code examples
excelvbatextboxuserform

Userform change the textbox colour based on cell value


I am trying to change the colour of the textbox in a userform based on the value in the textbox compared to the cell value in C10, i.e if textbox value is 9 and cell value is 10 the colour is green

the below code make the textbox the same colour regardless of values entered

Private Sub TextBox16_Change()
If ActiveSheet.Range("C10").Value > TextBox16.Value Then
    Me.TextBox16.ForeColor = &H8000000D
    Me.TextBox16.BackColor = &HFF&
Else
    Me.TextBox16.ForeColor = &HFF&
    Me.TextBox16.BackColor = &H8000000D
End If

End Sub

Thanks,


Solution

  • Try this If ActiveSheet.Range("C10").Value > CLng(TextBox16.Value) Then
    But you'll need to add some error handling because when textbox gets empty, or some other value than number you'll end up with 'type mismath' error.