Macro
How do I keep a specific macro only ran on a specific sheet, instead of doing it in every sheet in the workbook?
RE: Macro
Hi Wenyi,
Thank you for the forum question and thank you for the nice feedback.
To make a macro run only on a specific worksheet, you have a few solid options depending on how your macro is triggered.
________________________________________
Option 1: Check the sheet name inside your macro
Add a simple condition at the start of the macro:
Sub MyMacro()
If ActiveSheet.Name <> "Sheet1" Then Exit Sub
' Your macro code here
MsgBox "Running on Sheet1 only"
End Sub
``
This ensures the macro immediately stops if you're not on "Sheet1".
________________________________________
Option 2: Refer directly to a specific worksheet (best practice)
Instead of relying on the active sheet, explicitly target the correct one:
Sub MyMacro()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
' Example: only work on Sheet1
ws.Range("A1").Value = "Hello"
End Sub
This makes your macro ignore all other sheets entirely.
________________________________________
Option 3: Put the macro in the sheet module (for event macros)
If your macro is triggered by events (like change, selection, etc.), place it inside the specific sheet's code module, not in a standard module.
Example:
1. Right-click the sheet tab → View Code
2. Add something like:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "This only runs on this sheet!"
End Sub
This code will only trigger for that sheet, not others.
________________________________________
Option 4: Use Workbook events (if needed) + filter by sheet
If you're using Workbook_SheetChange, filter it:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name <> "Sheet1" Then Exit Sub
MsgBox "Change happened on Sheet1"
End Sub
If you need more macro or VBA knowledge, as I mentioned on the course, STL run 3 VBA courses. VBA Introduction, VBA Intermediate, and VBA Advanced.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector


