vba 仅当 Excel 用户窗体中的 CheckBox1 和 CheckBox2 = True 时激活按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22636569/
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
Activating a button only when CheckBox1 and CheckBox2 = True in Excel UserForm
提问by user3271518
So I am trying to make a UserForm where if both checkboxes are marked the button will be allowed to be clicked. I have tried a bunch of different code here is my first try.
所以我试图制作一个用户窗体,如果两个复选框都被标记,则允许单击该按钮。我尝试了一堆不同的代码,这是我的第一次尝试。
Private Sub CommandButton1_Click()
If CheckBox1.Value = True & CheckBox2.Value = True Then
CommandButton1.Enabled = True
Call GOGOGO
ElseIf CheckBox1.Value = True & CheckBox2.Value = False Then
MsgBox ("Run Characteristics Version 1.2.xlsm")
ElseIf CheckBox1.Value = False & CheckBox2.Value = True Then
MsgBox ("Log in")
Else
MsgBox ("Log in and Run Characteristics Version 1.2.xlsm")
End If
End Sub
Also Tried
也试过
Private Sub CheckBox1_Change()
'Evaluate the value of the CheckBox
Select Case CheckBox1.Value
Case True, False
End Select
End Sub
Private Sub CheckBox2_Change()
'Evaluate the value of the CheckBox
Select Case CheckBox2.Value
Case True, False
End Select
End Sub
Private Sub CommandButton1_Click()
Select Case CheckBox1.Value
Select Case CheckBox2.Value
If CheckBox1.Value = True & CheckBox2.Value = True Then
CommandButton1.Enabled = True
Call GOGOGO
ElseIf CheckBox1.Value = True & CheckBox2.Value = False Then
MsgBox ("Run Characteristics Version 1.2.xlsm")
ElseIf CheckBox1.Value = False & CheckBox2.Value = True Then
MsgBox ("Log in")
Else
MsgBox ("Log in and Run Characteristics Version 1.2.xlsm")
End If
End Select
End Select
End Sub
Both only give me my first ElseIf. So with all the different combinations of checkboxes when I click the commandbutton i always get msgBox "Run Characteristics...."
两者都只给我我的第一个 ElseIf。因此,当我单击命令按钮时,对于所有不同的复选框组合,我总是得到 msgBox“运行特征....”
Question: How Can I make a button In Excel UserForm only Run a command when 2 checkboxes are marked?
问题:如何仅在 Excel 用户窗体中创建按钮 标记 2 个复选框时运行命令?
回答by John Bustos
You were veryclose, your challenge is simply that you used &
rather than the VBA And
operator.
您非常接近,您的挑战只是您使用&
而不是 VBAAnd
运算符。
For example, your first sub should look as follows:
例如,您的第一个 sub 应如下所示:
Private Sub CommandButton1_Click()
If CheckBox1.Value = True And CheckBox2.Value = True Then
CommandButton1.Enabled = True
Call GoGoGo
ElseIf CheckBox1.Value = True And CheckBox2.Value = False Then
MsgBox ("Run Characteristics Version 1.2.xlsm")
ElseIf CheckBox1.Value = False And CheckBox2.Value = True Then
MsgBox ("Log in")
Else
MsgBox ("Log in and Run Characteristics Version 1.2.xlsm")
End If
End Sub
Note that all I did was replace your &
with the word And
.
请注意,我所做的只是将 your 替换&
为单词And
。
Hope that gives you what you need!!
希望能给你你需要的东西!!
回答by user116095
Do not use '&' in if statement. It is used for string concatenation. This should make your code work. Also note that the enable comment is illogically placed, as the button is already pushed.
不要在 if 语句中使用“&”。它用于字符串连接。这应该使您的代码工作。另请注意,启用注释的放置不合逻辑,因为按钮已被按下。
Private Sub CommandButton1_Click()
If (CheckBox1.Value And CheckBox2.Value) Then
Call GOGOGO
else
If not CheckBox1.Value Then
If not CheckBox2.Value then
MsgBox ("Log in and Run Characteristics Version 1.2.xlsm")
else
MsgBox ("Run Characteristics Version 1.2.xlsm")
end if
else
MsgBox ("Log in")
end if
end if
End Sub
For the 2nd method:
对于第二种方法:
Private Sub CheckBox1_Change()
if CheckBox1.Value and CheckBox2.Value
CommandButton1.Enabled = True
else
CommandButton1.Enabled = False
end if
End Sub
Private Sub CheckBox2_Change()
if CheckBox1.Value and CheckBox2.Value
CommandButton1.Enabled = True
else
CommandButton1.Enabled = False
end if
End Sub
But then you also have to initialize the commandbutton1
但是你还必须初始化 commandbutton1