Command button on/off

raw

Board Regular
Joined
Feb 5, 2003
Messages
63
Hi

I have a sheet with a couple of combo boxes and a command button.

Is there a way that would make the command button work only when a selection is made in a combo box

Eg combo box

Please select
Std bottom
Shaped finish
Straight or Shaped finish with trim

So if the bottom select is made (straight or shaped finish with trim) the command button when press will bring up a form i have created BUT if any of the other selections have been made pressing of the button will have no effect?

Thanks
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I would assign a value when for each option in an isolated location on the spreadsheet and then use if statement in the macro code to determine which code is run when the command button is selected.
 
Upvote 0
You can use an if statment in your VBA referring to the state of the combo box.

Somthing like this:

Private Sub CommandButton1_Click()

If CheckBox1.Value = False And CheckBox2.Value = False Then
MsgBox Prompt:="Select at least 1 Check Box", Buttons:=vbInformation + vbOKOnly

ElseIf CheckBox1.Value = True And CheckBox2.Value = True Then
MsgBox Prompt:="Checkbox1 and Checkbox2 Selected", Buttons:=vbOKOnly

ElseIf CheckBox1.Value = True Then
MsgBox Prompt:="Checkbox1 Selected", Buttons:=vbOKOnly

Else
CheckBox2.Value = True
MsgBox Prompt:="Checkbox2 Selected", Buttons:=vbOKOnly

End If

End Sub
 
Upvote 0
What you could also try is to keep the command button disabled, until entries are made in both comboboxes. Something like this:

In the ThisWorkbook code module:
Code:
Private Sub Workbook_Open()
    Worksheets("Sheet1").CommandButton1.Enabled = False
    Worksheets("Sheet1").ComboBox1.ListFillRange = "A1:A10"
    Worksheets("Sheet1").ComboBox2.ListFillRange = "B1:B10"
End Sub
That disables the button and set the ranges of values that are in the comboboxes.

And in the sheet module where the comboboxes and command button are located:
Code:
Private Sub ComboBox1_Change()
    If ComboBox1.Value = "" Or ComboBox2.Value = "" Then
        CommandButton1.Enabled = False
    Else
        CommandButton1.Enabled = True
    End If
End Sub

Private Sub ComboBox2_Change()
    If ComboBox1.Value = "" Or ComboBox2.Value = "" Then
        CommandButton1.Enabled = False
    Else
        CommandButton1.Enabled = True
    End If
End Sub

That will make sure both buttons have something in them before the command button gets enabled. Please post back if you have any questions. Hope that helps!
 
Upvote 0

Forum statistics

Threads
1,206,757
Messages
6,074,760
Members
446,084
Latest member
WalmitAal

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top