+ Reply to Thread
Results 1 to 4 of 4

Cell range protect/unprotect toggle

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-30-2007
    Location
    London, UK
    MS-Off Ver
    Microsoft Office 2007
    Posts
    317

    Smile Cell range protect/unprotect toggle

    Hi Guys,

    I wanted to have a piece of code that could that would create a toggle for Lock and Unlock for a range of cells.

    The code below unprotects the sheet so that I can set the protection for the range and then re-protects the sheet.

    Is there an If and Else command to say IF range (A8:B20) is locked then unlock ELSE lock?

    The reason I need this is that I have a budget spreadsheet and I may need to change the descriptions and price values of those ranges without having the rest of the worksheet vulnerable to being unlocked.

    Can someone help me out please, below is the code I have so far I just need the blanks filled in where it says 'My Code starts here'

    Thanks in advance.

    Sub Protectsheet()
    
    Dim Rng As Range
    Set Rng = Range("A8:B20")
            Sheet1.Unprotect "Password"
         'My Code starts here
    
          'My Code ends here
                Sheet1.Protect "Password"
                Sheet1.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowSorting:=True, AllowFiltering:=True
            wSht.EnableSelection = xlUnlockedCells
            
            
    End Sub
    Last edited by Zyphon; 02-21-2008 at 04:52 AM.
    Best Regards.

    Michael
    -----------------------------------
    Windows Vista, Microsoft Office 2007

  2. #2
    Forum Contributor
    Join Date
    03-30-2007
    Location
    London, UK
    MS-Off Ver
    Microsoft Office 2007
    Posts
    317

    Smile Solved! I managed to work it out

    Although I don't know how efficient my code is, I managed to work it out. I now have the code below, I also change the background colour of the cells depending whether they are locked or not.

    Of course all my knowledge gain here is from the homework from previous code that I have learned of you guys, so thank you so much for all your past efforts.

    Now I just need to tweak the code so that it can be called from muliple sheets.

    EDIT: Solved that also, changed all the 'Sheet1' references to 'ActiveSheet'.

    Below is the code:
    Sub Protect()
        Dim i As Integer
            Sheet1.Unprotect "Password"
         'My Code starts here
        If Range("A8:B20").Locked = False Then
        Range("A8:B20").Locked = True
        For i = 8 To 20
        Cells(i, 1).Interior.ColorIndex = 35
        Cells(i, 2).Interior.ColorIndex = 35
        Next i
        Else
        Range("A8:B20").Locked = False
        For i = 8 To 20
        Cells(i, 1).Interior.ColorIndex = 2
        Cells(i, 2).Interior.ColorIndex = 2
        Next i
          'My Code ends here
          End If
                Sheet1.Protect "Password"
                Sheet1.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowSorting:=True, AllowFiltering:=True
            Sheet1.EnableSelection = xlUnlockedCells
           
        End Sub
    Last edited by Zyphon; 02-21-2008 at 06:27 AM.

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

    Option Explicit
    
    Sub Protect()
        Dim i      As Integer
        Sheet1.Unprotect "Password"
        'My Code starts here
        With Range("A8:B20")
            If .Locked = False Then
                .Locked = True
                .Interior.ColorIndex = 35
                      Else: .Locked = False
                .Interior.ColorIndex = 2
                        'My Code ends here
            End If
        End With
        With Sheet1
            .Protect "Password"
            .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
                                                                      , AllowSorting:=True, AllowFiltering:=True
            .EnableSelection = xlUnlockedCells
        End With
    End Sub
    Hope that helps.

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

    Free DataBaseForm example

  4. #4
    Forum Contributor
    Join Date
    03-30-2007
    Location
    London, UK
    MS-Off Ver
    Microsoft Office 2007
    Posts
    317

    Smile

    @royUK

    Thank you so much, I knew you would be able to find a more efficient way for achieving my goal.

    I shall make the changes you suggest.

    Thanks again royUK.

+ 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