Vous êtes sur la page 1sur 2

Run different macros based on a ComboBox form list selection

'In your VBA project you need a UserForm (for us UserForm1) and modules (in this example we have one module: Module1) for your macros. '***************** '*****Module1***** '***************** 'Assign the following sub to the button that has to open the ComboBox with the list of options/macros available.

Sub BoxSelection() Userform1.Show End Sub Sub ChooseOption(strName As String) Userform1.UserForm_Close If strName = vbNullString Then Exit Sub Else Select Case strName Case "first choice" 'your macro based on first choice here

Case "second choice" 'your macro based on second choice here Case "third choice" 'your macro based on third choice here Case Else 'else macro here End Select End If End Sub '******************* '*****UserForm1***** '******************* 'This sub to populate the list with your options. Sub ComboBox1_Change() Userform1.ComboBox1.AddItem "first choice" Userform1.ComboBox1.AddItem "second choice" Userform1.ComboBox1.AddItem "third choice" End Sub 'This sub to assign macro "ChooseOption(strName As String)" in the "Module1" to the 'CommandButton in the UserForm1, and run the macro with your choice selected. Private Sub CommandButton1_Click() Module1.ChooseOption (ComboBox1.SelText) End Sub 'This sub to close the UserForm. Use only Unload Me and not UserForm1.Hide because this do not 'close the userform and the list of choices will be duplicated next time you run the sub BoxSelection(). Sub UserForm_Close() Unload Me End Sub

Vous aimerez peut-être aussi