+ Reply to Thread
Results 1 to 4 of 4

Unprotect a Worksheet in VB code. Excel 2007

Hybrid View

  1. #1
    Registered User
    Join Date
    11-06-2013
    Location
    Leeds
    MS-Off Ver
    Excel 2007
    Posts
    2

    Unprotect a Worksheet in VB code. Excel 2007

    Hi

    I have some code that hides rows in a spreadsheet but it doesn't work when I protect the worksheet. Can anybody tell me the code to unprotect the sheet before it runs the hide rows bit and then protect the sheet at the end of the code. Sorry I know this is probably really simple but I've only just started to write macros again after a loooooooooong break.

    Many thanks.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

    Set KeyCells = Range("REF_TYPE")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
    Is Nothing Then


    With Sheets("Referral Form")
    If Range("REF_TYPE_LKP").Value = 0 Then Rows("6:70").EntireRow.Hidden = True
    If Range("REF_TYPE_LKP").Value = 1 Then Rows("6:70").EntireRow.Hidden = False
    If Range("REF_TYPE_LKP").Value = 1 Then Rows("29:35").EntireRow.Hidden = True
    If Range("REF_TYPE_LKP").Value = 2 Then Rows("6:70").EntireRow.Hidden = False
    If Range("REF_TYPE_LKP").Value = 2 Then Rows("29:35").EntireRow.Hidden = False
    End With
    End If
    End Sub

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Unprotect a Worksheet in VB code. Excel 2007

    Hello and welcome to the forum....Moderators will probably tell you soon to put code brackets around code....See Rules to learn how....so, beyond that, try this...

    
        ActiveSheet.Unprotect
    
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    So one unprotects, one protects......by the way, I figured this out by using record macro....a great way to start....
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Unprotect a Worksheet in VB code. Excel 2007

    did this help?

  4. #4
    Registered User
    Join Date
    11-06-2013
    Location
    Leeds
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Unprotect a Worksheet in VB code. Excel 2007

    Hi. Sorry for the delay not had chance to try before now.

    It works a treat thank you. I probably should have said I have a password that I need to incorporate into the code. I've been trying to do that bit but not quite there yet.

    Thanks for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to adapt 'sort' code to unprotect/protect worksheet
    By nje in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-19-2013, 03:11 PM
  2. [SOLVED] I need a macro to unprotect worksheet before I run a sort code
    By Geller in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-28-2012, 09:07 AM
  3. VBA Code to Unprotect a Worksheet then Protect
    By aalbertson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-12-2011, 04:03 AM
  4. Any Way to Unprotect worksheet to see macro code
    By krishhi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2008, 04:45 AM

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