VBA for If one value is selected in a combo box ensure another combo box is completed

Lesgrimes

New Member
Joined
Oct 12, 2011
Messages
5
Hi, I'm very new to VBA and was wondering if somebody could help me with the following

I have a user form with multiple combo boxes and text boxes.

One of these combo boxes has three options, all entered via VBA

Private Sub ComboBoxCategory_DropButt*******()
'Populate control.
Me.ComboBoxCategory.AddItem "Beverage"
Me.ComboBoxCategory.AddItem "Food"
Me.ComboBoxCategory.AddItem "Hotel Supplies"
End Sub

If the option of Hotel Supplies is selected (and only this option), I want to ensure that a different combo box has one of its options selected.

I would like a message box to appear at time of selection not at the end when the command button for Save is clicked (I have a check for empty fields for everything else at this point)

Is this possible and if so, how do I go about creating the code for this?

Thanking you in advance for reading and hopefully replying
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this

Code:
Dim qClose As Boolean


Private Sub ComboBoxCategory_Change()
  If ComboBoxCategory.Value = "Hotel Supplies" Then
    If ComboBoxDifferent.ListIndex = -1 Then
      MsgBox "You must select an option en combo2"
      ComboBoxDifferent.SetFocus
    End If
  End If
End Sub


Private Sub ComboBoxDifferent_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If qClose Then Exit Sub
  If ComboBoxCategory.Value = "Hotel Supplies" Then
    If ComboBoxDifferent.Value = "" Or ComboBoxDifferent.ListIndex = -1 Then
      MsgBox "You must select an option"
      Cancel = True
    End If
  End If
End Sub


Private Sub UserForm_Activate()
  Me.ComboBoxCategory.AddItem "Beverage"
  Me.ComboBoxCategory.AddItem "Food"
  Me.ComboBoxCategory.AddItem "Hotel Supplies"
  
  ComboBoxDifferent.AddItem "option1"
  ComboBoxDifferent.AddItem "option2"
  ComboBoxDifferent.AddItem "option3"
  
  qClose = False
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  qClose = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

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