Excel根据值更改文本框颜色 [英] Excel change text box color based on value

查看:61
本文介绍了Excel根据值更改文本框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据从另一个页面中检索到的文本框中的值来更改文本框的背景颜色.我发现,您无法使用文本框进行条件格式设置,因此唯一的方法是使用VBA.我一辈子都无法弄清楚如何设置代码来完成这项工作.我尝试打开VBA查看器并使用以下代码:

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

推荐答案

我假设您使用的是用户窗体,在该窗体中需要文本框根据其中的信息来更改颜色.

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:

对于UserForm1,其中列出了1-4个4个文本框

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

最终结果如下:

这篇关于Excel根据值更改文本框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆