+ Reply to Thread
Results 1 to 4 of 4

Protect/Unprotect code causing out of range error in code

  1. #1
    Registered User
    Join Date
    01-18-2010
    Location
    CT
    MS-Off Ver
    Excel 2003
    Posts
    17

    Protect/Unprotect code causing out of range error in code

    Guys,

    I was hoping someone could me identify why this is happening. I have a macro code which sorts a defined set of rows based on a particular column. Likewise, there is a macro that unsorts them back to their original positions. Now the macros themselves work perfectly, however, the sheet needs to be protected before distribution. When I input the code to protect/unprotect the sheets before and after the macro has run, I get a subscript out of range error.

    The name of the sheet in question is OER Dashboard Report and the password is Grimmer

    Here is an example of one of the two macros that work perfectly:

    Sub sortstores()
    'Change first range as needed
    'Do not change Key1
    Range("C11:Y89").Sort Key1:=Range("F1"), _
    Order1:=xlDescending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, _
    DataOption1:=xlSortTextAsNumbers
    End Sub



    The subscript out of range error occurs when I change the code to this:



    Sub sortstores()
    Sheets("OER Dashboard Report").Unprotect Password:="Grimmer"
    'Change first range as needed
    'Do not change Key1
    Range("C11:Y89").Sort Key1:=Range("F1"), _
    Order1:=xlDescending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, _
    DataOption1:=xlSortTextAsNumbers
    Sheets("OER Dashboard Report").Protect Password:="Grimmer"
    End Sub



    Any ideas??
    Last edited by Petrolcb41; 12-02-2010 at 02:26 PM.

  2. #2
    Forum Contributor
    Join Date
    11-25-2010
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    120

    Smile Re: Protect/Unprotect code causing out of range error in code

    Hi there,

    Try changing it to:

    Please Login or Register  to view this content.
    And similar for protect.
    If the post was helpful please click the black star on the bottom left to add some reputation and mark your thread as SOLVED.

    A day with nothing new achieved or learned, albeit however small, is a day lost forever?

    Constant Never Ending Improvement

  3. #3
    Registered User
    Join Date
    01-18-2010
    Location
    CT
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Protect/Unprotect code causing out of range error in code

    Lifesigns,

    You are the best! Totally worked. This has been driving me absolutely insane. Thanks so much for your prompt response

  4. #4
    Forum Contributor
    Join Date
    11-25-2010
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    120

    Re: Protect/Unprotect code causing out of range error in code

    Hey no problem, I had a workbook with 25 sheets that needed protection and doing it manually was just crazy

    Keep well!

+ 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