vba 如何使用按钮更改文本框的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24297282/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:31:00  来源:igfitidea点击:

How to Change background Colour of Textbox Using a Button

excelvbacolorstextbox

提问by user3750509

I am trying to create an Excel file where I have a macro that changes the fill colour of a textbox. This macro would be activated when the user clicks on a Button (Form Control). I am fairly new to VBA (and programming in general), and am having trouble writing the code for this. I've looked through other forums and tried applying what I read, but everybody seems to solve the problems using different code structure and/or syntax. The logic for my code is essentially as follows:

我正在尝试创建一个 Excel 文件,其中我有一个更改文本框填充颜色的宏。当用户单击按钮(表单控件)时,将激活此宏。我对 VBA(以及一般的编程)相当陌生,并且在为此编写代码时遇到了麻烦。我浏览了其他论坛并尝试应用我阅读的内容,但每个人似乎都使用不同的代码结构和/或语法来解决问题。我的代码的逻辑基本上如下:

User clicks on button to activate macro:

用户点击按钮激活宏:

If TextBox1 BackColor = RGB (191, 191, 191) 

     Then TextBox1 BackColor = RGB(242, 242, 242)

Else if TextBox BackColor = RGB(242, 242, 242)

     Then TextBox1 BackColor = RGB(191, 191, 191)

回答by VBlades

I've tried this and this should work for you. I attached it to a button and it worked fine:

我试过这个,这应该对你有用。我把它附加到一个按钮上,它工作正常:

If TextBox1.BackColor = RGB(191, 191, 191) Then
     TextBox1.BackColor = RGB(242, 242, 242)
ElseIf TextBox1.BackColor = RGB(242, 242, 242) Then
     TextBox1.BackColor = RGB(191, 191, 191)
End If

You use the dot (.) to get to an object's properties, so TextBox1.BackColor.

您使用点 (.) 来获取对象的属性,例如 TextBox1.BackColor。