Excel run different macros when different cells are selected

mrstarfire

New Member
Joined
Apr 16, 2018
Messages
2
Hi, using excel I need to be able to run different macros when different cells are selected (clicked) in a worksheet.
For example.
cell e1 run macro save
cell f1 run macro print this page
cell B4 run the macro update 4
cell b5 run the macro update 5
cell d9 run the macro update 15

I have tried using this code in the worksheet.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("e1")) Is Nothing Then
save
End If
End If
End Sub

which allows me to save the spreadsheet but it wont allow me to run any other macros.

I tried adding this as well


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("b4")) Is Nothing Then
update4
End If
End If
End Sub

When I try i get this error message.
Compile error
Ambiguous name detected: Worksheet_SelectionChange
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You can use something like this:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Count <> 1 Then Exit Sub

Select Case Target.Address
    Case "$E$1"
        Call Save
    Case "$F$1"
        Call PrintThisPage
    Case "$B$4"
        Call Update4
    Case "$B$5"
        Call Update5
    Case "$D$9"
        Call Update15
End Select

End Sub

WBD
 
Upvote 0
Hi, thanks for replying, Im sorry but I ment to say that I need different things to run when different cells are clicked.

When Cell E1 is clicked, save the sheet
When cell F1 is clicked, print the sheet
When Cell B4 is clicked, run update4
When Cell D9 is clicked, run update 15



You can use something like this:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Count <> 1 Then Exit Sub

Select Case Target.Address
    Case "$E$1"
        Call Save
    Case "$F$1"
        Call PrintThisPage
    Case "$B$4"
        Call Update4
    Case "$B$5"
        Call Update5
    Case "$D$9"
        Call Update15
End Select

End Sub

WBD
 
Upvote 0
That's just an example. Under each "Case" statement, just put the code you want to run. e.g.

Code:
Case "$E$1"
    ActiveWorkbook.Save
Case "$F$1"
    ActiveSheet.Printout

etc.

WBD
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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