Can I restrict which macros will run by active sheet? (1 Viewer)

TomH

Registered User.
Local time
Yesterday, 19:17
Joined
Nov 3, 2008
Messages
111
I have a workbook with several sheets in it. Each sheet has macros to perform certain functions, like copy/paste, move input data to a table, delete certain cells' contents, etc. I'm concerned about hitting the wrong shortcut for the macros and running the wrong process on the wrong sheet.

Is there a way of indicating in a macro to not run if the active sheet is not the one it was designed for?

THANKS for any help!
 

Rx_

Nothing In Moderation
Local time
Yesterday, 17:17
Joined
Oct 22, 2009
Messages
2,803
Instead of running the code direct, run from a centralized subroutine.
The routine checks to see what sheet is the ActiveSheet.
The Select Case takes the name and determines what code should be run
Something like this in a Standard Module.
Just prototype code as an example.

Dim MySheet
Sub WhatThisSheet()
Mysheet = ActiveSheet.name
End Sub

' write code Routine Sub1, Sub2, Sub3

Sub TheRuns ' Depending on the Sheet, what routine runs?
On Error GoTo Lou
WhatThisSheet
Select Case MySheet
Case "Sheet1"
Sub1
Sub2
Case Else
Sub3
End Select
Exit Sub
Lou:
Debug.Print "clean up this mess " & Err.Description
End Sub

Another way is to use IF / Then . Here is a rough idea:
Code:
Sub check() 
    If InStr(",Sheet1,Sheet2,Sheet5,Sheet9,", "," & ActiveSheet.CodeName & ",") > 0 Then
       MsgBox "This action is for specific Worksheets only!" 
   else
      ' your code
   end if

End Sub
 
Last edited:

Brianwarnock

Retired
Local time
Today, 00:17
Joined
Jun 2, 2003
Messages
12,701
As I understand it Tom does not want to run any code if he selects the wrong shortcut. Having determined the active sheet name he should compare this with the name stored in the macro and act appropriately, ie run the macro or issue a message and exit.

I never ran against Activesheet but always ran against named sheets, it might only be a remote possibility but if code runs slowly you may switch sheets whilst it is running and thus cause chaos, but that is not the issue here I think.

Brian
 

Rx_

Nothing In Moderation
Local time
Yesterday, 17:17
Joined
Oct 22, 2009
Messages
2,803
LOL - yes That is why I often prototype a code example. If they come back with specifics, then actual code might be worth our times. Otherwise, there are plenty of other people to investo our time to help.
 

TomH

Registered User.
Local time
Yesterday, 19:17
Joined
Nov 3, 2008
Messages
111
As common with Excel no response from the OP.

Sorry. I don't have the opportunity to check back as often as I would like.

And, yes, you are correct. I would like to make it so that if I run the wrong shortcut, perhaps the only thing that happens is that I get a dialog box telling me so. So, I guess there has to be some comparison of the active sheet name to a reference in the macro itself... some kind of IF-THEN?

Thanks again for the assistance.
 

Users who are viewing this thread

Top Bottom