Reputation: 628
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...."
Question: How Can I make a button In Excel UserForm only Run a command when 2 checkboxes are marked?
Upvotes: 0
Views: 10588
Reputation: 1
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.
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
Upvotes: 0
Reputation: 19574
You were very close, your challenge is simply that you used &
rather than the VBA And
operator.
For example, your first sub should look as follows:
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
.
Hope that gives you what you need!!
Upvotes: 1