用户窗体根据单元格值更改文本框颜色

Userform change the textbox colour based on cell value

我正在尝试根据文本框中的值与 C10 中的单元格值相比更改用户窗体中文本框的颜色,即如果文本框值为 9 且单元格值为 10,则颜色为绿色

无论输入的值如何,下面的代码都会使文本框颜色相同

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

谢谢,

试试这个 If ActiveSheet.Range("C10").Value > CLng(TextBox16.Value) Then
但是您需要添加一些错误处理,因为当文本框变空或出现除数字以外的其他值时,您最终会遇到 'type mismath' 错误。