我正尝试编写一个Excel宏来根据表格中单元格的输入值自动更改文本框的颜色。目前我拥有的代码如下:
Private Sub TextBox1_Change()
'Declare Variables
Dim cell As Range
Dim color As String
'Initialize Variables
Set cell = Range("A1")
color = cell.Value
'Set TextBox Color
If color = "" Then TextBox1.BackColor = RGB(255, 255, 255) 'white
If color = "1" Then TextBox1.BackColor = RGB(255, 0, 0) 'red
If color = "2" Then TextBox1.BackColor = RGB(0, 255, 0) 'green
If color = "3" Then TextBox1.BackColor = RGB(0, 0, 255) 'blue
End Sub
这段代码的作用是读取A1单元格的数值,然后根据这个数值改变文本框的颜色。我的代码确实成功改变了文本框的颜色,但只有在我点击文本框并输入内容后才会更新。有没有办法在A1单元格输入数值时立即更新颜色?
如果使用其他对象更容易实现,我不一定非要使用文本框,但不能仅仅使用单元格。