How can I restrict a macro to only work in one sheet?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
I already have the code in workbook module, but I have assigned it to a keyboard shortcut (Ctrl+d) and that works in all of the sheets in that workbook.

What's the recommended way to restrict the macro so that it only runs when called from one sheet?

Can I restrict the shortcut to just one sheet?

If not, can I query the sheet name in the macro and if not the right sheet, either just exit or issue a warning message?

If I do that, should I use the raw sheet name (sheet26) or the name I have assigned to it. I am assuming the raw name is safer and will still work if I should happen to rename the sheet.

Thanks for any suggestions.
 
In that code wherever you have Range it will refer to a range on the active sheet, not any specific sheet.

Yes, I understand. That's why I want to make sure that the macro won't run if called from any other sheet.
 
Upvote 0

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.
Which sheet do you want the code to run on and which sheet do you want to run it from?
 
Upvote 0
I would use the code name:

Code:
If Not activesheet is sheet1 Then
   msgbox "Wrong sheet active!"
   Exit Sub
End If

for example.
 
Upvote 0
Which sheet do you want the code to run on and which sheet do you want to run it from?

I want it to run from a particular sheet (eg sheet13) and to run on that same sheet. I think I have it. See me response to RoryA below.
 
Upvote 0
I would use the code name:

Code:
If Not activesheet is sheet1 Then
   msgbox "Wrong sheet active!"
   Exit Sub
End If

for example.

That was my initial thinking. That way, if I change the name of the sheet, I don't have to remember to change the macro. But after more consideration, I have decided to use the name. I frequently make a copy of a sheet during development so I can go back if I don't like the changes. Depending on which way the copy goes, that could result in the active sheet having a different codename. For now, I'm going with the name.

Code:
. . .

Const SheetName As String = "1 loss"      'The name of the sheet that can use this sub
If ActiveSheet.Name <> SheetName Then
  MsgBox "This macro can only be called from '" & SheetName & "'", vbOKOnly, SubName
  Exit Sub
End If

. . .

Thanks to everyone for the help. It seems to be working until the next time I break it. ;-)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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