vba Excel根据值更改文本框颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30624544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel change text box color based on value
提问by HelloWor1d
I need to change the background color of a text box based on the value in that box which is being retrieved from another page. As I am finding out, you cannot do conditional formatting with text boxes, so the only way is with VBA. I cannot for the life of me figure out how to set up the code to make this work. I tried opening the VBA viewer and using this code:
我需要根据正在从另一个页面检索的文本框中的值更改文本框的背景颜色。正如我发现的那样,您不能使用文本框进行条件格式设置,因此唯一的方法是使用 VBA。我一生都无法弄清楚如何设置代码来完成这项工作。我尝试打开 VBA 查看器并使用以下代码:
Private Sub TextBox1_Change()
If TextBox1.Value = "" Or Not "1" Or Not "2" Or Not "3" Then _
TextBox1.BackColor = RGB(0, 0, 0)
If TextBox1.Value = "1" Then TextBox1.BackColor = RGB(255, 0, 0)
If TextBox1.Value = "2" Then TextBox1.BackColor = RGB(0, 255, 0)
If TextBox1.Value = "3" Then TextBox1.BackColor = RGB(0, 0, 255)
End Sub
I get some error that says Object required and one that says block if without end if? I found the code on a forum and the user had success, so I know it should work. Thanks in advance
我收到一些错误,说需要对象,还有一个说如果没有结束则阻止?我在论坛上找到了代码并且用户成功了,所以我知道它应该可以工作。提前致谢
回答by R3uK
This should help you :
这应该可以帮助您:
Private Sub TextBox1_Change()
With Sheets("Sheet3").OLEObjects("TextBox1").Object
Select Case .Value
Case Is = vbNullString 'same as ""
.BackColor = RGB(0, 0, 0)
Case Is = 1
.BackColor = RGB(255, 0, 0)
Case Is = 2
.BackColor = RGB(0, 255, 0)
Case Is = 3
.BackColor = RGB(0, 0, 255)
Case Else
.BackColor = RGB(126, 126, 126)
End Select
End With
End Sub
回答by LockeGarmin
You are getting an Object required error because you put your code in an object where Textbox1 doesn't exist. Put your code into either a userform or a worksheet where there is a Textbox named "Textbox1".
您收到 Object required 错误,因为您将代码放在 Textbox1 不存在的对象中。将您的代码放入用户表单或工作表中,其中有一个名为“Textbox1”的文本框。
To solve the "If without End If" issue change the following:
要解决“If without End If”问题,请更改以下内容:
If TextBox1.Value = "" Or Not "1" Or Not "2" Or Not "3" Then _
TextBox1.BackColor = RGB(0, 0, 0)
To this:
对此:
If TextBox1.Value = "" Or Not "1" Or Not "2" Or Not "3" Then _
TextBox1.BackColor = RGB(0, 0, 0)
I would personally use a Select Case statement like the following, but what you have works other than that one line.
我个人会使用像下面这样的 Select Case 语句,但是除了那一行之外,你所拥有的东西也有效。
Private Sub TextBox1_Change()
Select Case TextBox1.Value
Case "1": TextBox1.BackColor = RGB(255, 0, 0)
Case "2": TextBox1.BackColor = RGB(0, 255, 0)
Case "3": TextBox1.BackColor = RGB(0, 0, 255)
Case Else: TextBox1.BackColor = RGB(255, 255, 255)
End Select
End Sub
回答by Flaniganga
I am assuming that you are using a User form where you need the Textbox to change color based on the information in it.
我假设您正在使用用户表单,您需要文本框根据其中的信息更改颜色。
I am currently working on the same thing and I have come with the following solution:
我目前正在做同样的事情,我提出了以下解决方案:
For UserForm1 with 4 textboxes listed 1 - 4
对于列出 1 - 4 个文本框的 UserForm1
Private Sub Textbox1_Change()
If TextBox1.Text = "A" Then
TextBox1.BackColor = RGB(0, 32, 96)
ElseIf TextBox1.Text = "B" Then
TextBox1.BackColor = RGB(0, 112, 192)
ElseIf TextBox1.Text = "C" Then
TextBox1.BackColor = RGB(189, 215, 238)
ElseIf TextBox1.Text = "D" Then
TextBox1.BackColor = RGB(0, 176, 240)
End If
End Sub
Private Sub Textbox2_Change()
If TextBox2.Text = "A" Then
TextBox2.BackColor = RGB(0, 32, 96)
TextBox2.Font.Color = RGB(0, 0, 0)
ElseIf TextBox2.Text = "B" Then
TextBox2.BackColor = RGB(0, 112, 192)
ElseIf TextBox2.Text = "C" Then
TextBox2.BackColor = RGB(189, 215, 238)
ElseIf TextBox2.Text = "D" Then
TextBox2.BackColor = RGB(0, 176, 240)
End If
End Sub
Private Sub Textbox3_Change()
If TextBox3.Text = "A" Then
TextBox3.BackColor = RGB(0, 32, 96)
ElseIf TextBox3.Text = "B" Then
TextBox3.BackColor = RGB(0, 112, 192)
ElseIf TextBox3.Text = "C" Then
TextBox3.BackColor = RGB(189, 215, 238)
ElseIf TextBox3.Text = "D" Then
TextBox3.BackColor = RGB(0, 176, 240)
End If
End Sub
Private Sub Textbox4_Change()
If TextBox4.Text = "A" Then
TextBox4.BackColor = RGB(0, 32, 96)
ElseIf TextBox4.Text = "B" Then
TextBox4.BackColor = RGB(0, 112, 192)
ElseIf TextBox4.Text = "C" Then
TextBox4.BackColor = RGB(189, 215, 238)
ElseIf TextBox4.Text = "D" Then
TextBox4.BackColor = RGB(0, 176, 240)
End If
End Sub
The final result is as follows:
最终结果如下:


