+ Reply to Thread
Results 1 to 2 of 2

Thread: Only allow macro to run in certain worksheets

  1. #1
    Registered User
    Join Date
    03-13-2010
    Location
    Bedfordshire, England
    MS-Off Ver
    Excel 2003
    Posts
    49

    Only allow macro to run in certain worksheets

    I have a whole load of sort macros that are all similar to the one below and run by selecting a button on a custom toolbar. I only want certain macros to be enabled in certain worksheets to prevent errors. Is there some code I can add to each macro to check which worksheets are active before allowing the macro to run


    Range("A6:GR45").Select
        Selection.Sort Key1:=Range("B6"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
            Columns("GS:GT").EntireColumn.Hidden = True
            ActiveWindow.ScrollColumn = 3
        Range("A1:A5").Select
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Re: Only allow macro to run in certain worksheets

    try following solution [copy it to module for test and make sure that you have sheets with similar names]:
    Public Const inc = "Sheet1;Sheet 2;Some name" 'here sheets names you allow to run your code
    
    Sub test()
    t = Split(inc, ";")
    For i = 0 To UBound(t)
     If LCase(t(i)) = LCase(ActiveSheet.Name) Then
        check = True
        Exit For
     End If
    Next i
    
    If check = True Then
     'here your code
     MsgBox ActiveSheet.Name 'example run
    End If
    End Sub
    Best Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0