+ Reply to Thread
Results 1 to 5 of 5

Restricting Macro to Selected Sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-05-2010
    Location
    Exeter, England
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    180

    Restricting Macro to Selected Sheet

    Hi, The following macro inserts a template from "room template" into "estimate" which is assigned to toolbar button. The problem I have is that if a user presses the button regardless of what sheet they have active the macro runs and unlocks the "active sheet" and then errors as it can't perform the rest of the function and everything is unlocked! What I would like to achieve is that : If the user is not on the "estimating sheet" then a message box appears stating "not allowed" with just a cancel button and the macro does not run.


    ActiveSheet.Unprotect "password"
        
        
        Sheets("Room Template").Visible = True
        Sheets("Room Template").Select
        Rows("1:18").Select
        Selection.Copy
        
        Sheets(" Estimate").Select
        Application.EnableEvents = False
        Selection.Insert Shift:=x1Down
        Application.EnableEvents = True
        ActiveSheet.Protect "password", DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowInsertingRows:=True, AllowDeletingRows:=True
        Sheets("Room Template").Visible = xlSheetVeryHidden
        
        
            
    End Sub
    Last edited by zimbo109; 03-23-2010 at 05:17 PM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Restricting Macro to Selected Sheet

    You could pop this at the start of your code:

    If ActiveSheet.Name <> "Estimate" Then
    
        MsgBox "This can only be run on the Estimate sheet!", vbCritical
        Exit Sub
        
    End If

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Contributor
    Join Date
    02-05-2010
    Location
    Exeter, England
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    180

    Re: Restricting Macro to Selected Sheet

    Thanks Dom,

    Turning this on its head a bit, is there a way that the same macro could run regardless of what sheet is active. Obviostly the line "Sheets estimate.select" would have to change.

    So in effect the macro/button could insert the template in any sheet?

    Many thanks
    Chris

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Restricting Macro to Selected Sheet

    A couple of things this macro can show.

    1) You don't have to unprotect a sheet for VBA to act on it. You simply need to reapply the protection with a non-persistent flag UserInterfaceOnly:=True turned on. This keeps the protection active for the user, but not to VBA.

    2) You don't need to "select" things to act on them via VBA. This macro shows how to address those ranges directly using a complete object address, so your macro never leaves the activesheet.

    Option Explicit
    
    Sub test()
    
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    ws.Protect "password", DrawingObjects:=True, Contents:=True, Scenarios:=True, _
        AllowInsertingRows:=True, AllowDeletingRows:=True, UserInterfaceOnly:=True
    
        With Sheets("Room Template")
            .Visible = True
            .Rows("1:18").Copy
                Application.EnableEvents = False
                    Selection.Insert xlShiftDown
                Application.EnableEvents = True
            .Visible = xlSheetVeryHidden
        End With
    
    End Sub
    Last edited by JBeaucaire; 03-23-2010 at 07:17 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Contributor
    Join Date
    02-05-2010
    Location
    Exeter, England
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    180

    Re: Restricting Macro to Selected Sheet

    Thanks Jerry - that works, your help has been fantastic!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1