0% found this document useful (0 votes)
58 views2 pages

Run Different Macros Based On ComboBox

This document describes how to run different macros based on a combobox selection in a userform. It includes: 1) A module that contains a "ChooseOption" sub to run different macros based on the selected combobox option. 2) A userform that populates the combobox list and runs the "ChooseOption" macro on a command button click, passing the selected option. 3) The userform also contains a sub to close itself to prevent duplicating the combobox options.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

Run Different Macros Based On ComboBox

This document describes how to run different macros based on a combobox selection in a userform. It includes: 1) A module that contains a "ChooseOption" sub to run different macros based on the selected combobox option. 2) A userform that populates the combobox list and runs the "ChooseOption" macro on a command button click, passing the selected option. 3) The userform also contains a sub to close itself to prevent duplicating the combobox options.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like