cal macro

needhelp2

Active Member
Joined
Apr 19, 2011
Messages
250
Hi,

I want to know that how can i call macro to run automatically when a value select from drop down menu.
Like i have drop down menu in which i have name smith
i had made macro that when i select amazon when i copy some values from other sheets and place it under smith in same sheet where smith is selected

But i am facing issue when ever i select smith macro wont run automatically. i want that how macro run automatically when i select smith.matcros for smith has been saved with the name of smith

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
What kind of drop down are you using. Validation, Filter, Form control?
 
Upvote 0
Use Application.Run method and pass it macro name. For example, in Worksheet_Change event check every time whether cell with macro name has been changed. If so, then call Application.Run.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.Run Range("A1") 'Let A1 hold text "RunSub"
End Sub

Sub RunSub()
    MsgBox "I was executed!"
End Sub
 
Upvote 0
I would go for this. But if your using a filter it won't run the change event as it doesn't recognise the cell as being changed. Use a validation or a form tool to poulate a cell that will make the change event run.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Call YourCode
End If
End Sub

Sub YourCode()
MsgBox "I was executed!"
End Sub
 
Upvote 0
You can add DropDown control programmatically and select values from there to run macro (when double-click cell).
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Sub FillDropDown()

    Dim dd As DropDown

    With ActiveCell
        
        ' Add drop down control into active cell.
        Set dd = ActiveSheet.DropDowns.Add(.Left, .Top, .Width, .Height)
    
    With dd
        .DropDownLines = 30
        .Name = "dd1"
        
        ' Macro to run
        .OnAction = "RunSub"
        
        ' Add items. Use any source. Can also use List property as Combobox.
        .AddItem "Smith"
        .AddItem "John"
        
    End With

End Sub

Sub RunSub()

    With ActiveSheet.DropDowns(Application.Caller)
        Application.Run .List(.ListIndex) 'This gives selected value and pass to Run.
        .Delete
    End With

End Sub
 
Upvote 0
In previos code remove line "Sub FillDropDown()".
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,920
Latest member
jaspers

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