问题
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:
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
回答1:
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".
To solve the "If without End If" issue change the following:
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.
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
回答2:
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
回答3:
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
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:
来源:https://stackoverflow.com/questions/30624544/excel-change-text-box-color-based-on-value