+ Reply to Thread
Results 1 to 4 of 4

Help with macro, automatical locking cells when excel is closed but only locking 1 sh

Hybrid View

  1. #1
    Registered User
    Join Date
    12-30-2010
    Location
    Huddersfield, England
    MS-Off Ver
    Excel 2010
    Posts
    58

    Help with macro, automatical locking cells when excel is closed but only locking 1 sh

    I have a macro that locks cells once excel is closed, the only problem is that it only works on the first sheet, is there a way for all the sheets to do this, or just one for the wookbook?

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim SH As Worksheet
    Dim rng As Range
    Const PWORD As String = "ABC" '
    
    Set SH = Me.Sheets("SS Record Sheet") '
    
    With SH
    .Unprotect Password:=PWORD
    On Error Resume Next
    Set rng = SH.Cells.SpecialCells(xlCellTypeConstants)
    On Error GoTo 0
    
    If Not rng Is Nothing Then
    .Cells.Locked = False
    rng.Cells.Locked = True
    .Protect Password:=PWORD
    End If
    End With
    End Sub
    Private Sub Workbook_Open()
    'check for filter, turn on if none exists
    With Worksheets("SS Record Sheet")
      If Not .AutoFilterMode Then
        .Range("A1").AutoFilter
      End If
      .EnableAutoFilter = True
      .Protect Password:="ABC", _
      Contents:=True, UserInterfaceOnly:=True
    End With
    End Sub
    Last edited by snoopy1461; 01-21-2011 at 11:56 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,162

    Re: Help with macro

    Changes to your code are minimal but may look bigger because I added appropriate indentation.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
       Dim SH As Worksheet
       Dim rng As Range
       Const PWORD As String = "ABC" '
       
       'Set SH = Me.Sheets("SS Record Sheet") '
       For Each SH In Worksheets ' added
       
          With SH
             .Unprotect Password:=PWORD
             On Error Resume Next
             Set rng = SH.Cells.SpecialCells(xlCellTypeConstants)
             On Error GoTo 0
             
             If Not rng Is Nothing Then
                .Cells.Locked = False
                rng.Cells.Locked = True
                .Protect Password:=PWORD
             End If
          End With
       Next SH ' added
    
    End Sub
    [/quote]
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Help with macro

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    12-30-2010
    Location
    Huddersfield, England
    MS-Off Ver
    Excel 2010
    Posts
    58

    Re: Help with macro

    Fantastic, work perfectly.

+ 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