Run A Macro From A Drop Down List (VBA)
Run A Macro From A Drop Down List (VBA)
This article demonstrates how to execute a VBA macro using a Drop Down
list. The Drop Down list contains two macro names Macro1 and Macro 2,
however, you can easily add more if you like.
The animated picture below shows a Drop Down list containing two text
strings, Macro1 and Macro2. When I select Macro1 in cell B2 a worksheet
event code reads the selected value and runs a macro.
The example macros display a message box with the message Macro1 or
Macro2 based on the selected value.
Watch this video where I explain how to run a macro
from a drop down list
Below are detailed instructions on how I did it.
4. Click on the Drop Down list below "Allow:" and select "List".
5. Type your macro names in Source: field, separated by a comma.
6. Click OK button.
3. The Visual Basic Editor opens. Copy VBA event code below.
4. Paste to worksheet module.
5. Exit VB Editor (Alt + Q).
Note, save the workbook with file extension *.xlsm (macro enabled) to
attach the code to your workbook. This will save the workbook and the
code so you can use it the next you open the workbook.
Welcome! I am Oscar and here to help you out.
Related articles
Change chart data range using a Drop Down List [VBA]
In this article I will demonstrate how to quickly change chart data range
utilizing a combobox (drop-down list). The above […]
Add or remove a value in a drop down list programmatically
This article demonstrates how to add or remove a value in a regular drop
down list based on a list […]
Excel calendar [VBA]
This workbook contains two worksheets, one worksheet shows a calendar
and the other worksheet is used to store events. The […]
Show / hide a picture using a button
This article explains how to hide a specific image in Excel using a shape as
a button. If the user […]
2. Sam says:
3. Isuru says:
4. Carolyn Vermeulen says:
o Oscar says:
5. Mohamed Zaher says:
6. Rose says:
April 13, 2016 at 9:10 pm
The macro's worked great from the drop down list. I wanted to hide certain
rows for ease of use on a form. I tried adding a second drop down and vba
change coding, but without success. I am new to vba, so perhaps did it
incorrectly. So,instead of the second drop down list, I added a filter to the
form, but when the filter is used, the lines hidden by the original macro
unhide.
Is there a way to either run two different macro combinations from two
different drop lists, or alternately use the filter feature in conjunction with
one macro/drop down?
Thanks.
Reply
7. James says:
8. Bruno says:
o Oscar says:
9. Bruno says:
o Oscar says:
February 2, 2017 at 11:43 am
Lee Wood
Have you tried the downloaded excel file attached to this post?
It might give a clue what is wrong with you workbook.
Make sure you spell the macro name correctly in your vba code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2")) Is Nothing Then
Select Case Range("B2")
Case "Macro1": Macro1
Case "Macro2": Macro2
End Select
End If
End Sub