+ Reply to Thread
Results 1 to 5 of 5

Macro does not work with protected sheet

  1. #1
    Registered User
    Join Date
    11-11-2020
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    46

    Macro does not work with protected sheet

    Hi there,

    I have a macro that transfer disposed items to another sheet.
    However, it appears that when I lock the cells on both sheets (D4:D500) and (F4:F500), the macro button (Archive Disposed) won't run and has an error of 400.

    Currently I have this macro in my workbook:

    Option Explicit
    Sub TestKen()

    Dim ws As Worksheet

    Application.ScreenUpdating = False

    For Each ws In Worksheets
    If ws.Name <> "Sheet2" Then
    With ws.[A4].CurrentRegion
    .AutoFilter 6, "Disposed"
    .Offset(1).EntireRow.Copy Sheet2.Range("A" & Rows.Count).End(3)(2)
    .Offset(1).EntireRow.Delete
    .AutoFilter
    End With
    End If
    Next ws


    Application.ScreenUpdating = True

    End Sub


    Any idea how to solve this issue?
    Password I set for the worksheets are 123
    I assume I need a macro that unprotects the sheets, run the macro, and then re-protect the sheets again?

    Thanks in advance
    Cheers Ken
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Macro does not work with protected sheet

    Hi Ken,

    I assume I need a macro that unprotects the sheets, run the macro, and then re-protect the sheets again?
    Yes and note the following as well:
    1. There is no worksheet tab using Excel's GUI called "Sheet2". I think you mean the code name so I have changed that line of code accordingly.
    2. There were two "TestKen" macros - one at the workbook level and the other at the module level. I deleted the workbook version and put the following into "Module1" which ran without issue. Note, I think one (or both) of these were causing the Error 400 message as usually the error number when sheet protection clashes with code is 1004.

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    11-11-2020
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    46

    Re: Macro does not work with protected sheet

    Thanks a lot for this Robert!
    May I just ask (from an education point of view), what does the following highlighted section mean?

    Option Explicit
    Sub TestKen()

    Dim ws As Worksheet

    Application.ScreenUpdating = False

    For Each ws In Worksheets
    If ws.CodeName <> "Sheet2" Then 'References the code name of the tab.
    ws.Unprotect Password:=123
    With ws.[A4].CurrentRegion
    .AutoFilter 6, "Disposed"
    .Offset(1).EntireRow.Copy Sheet2.Range("A" & Rows.Count).End(3)(2)
    .Offset(1).EntireRow.Delete
    .AutoFilter
    End With
    ws.Protect Password:=123, DrawingObjects:=True, Contents:=True
    End If
    Next ws (Why need this Next ws? I thought <> "Sheet2" already means all the other sheets?)

    Application.ScreenUpdating = True

    End Sub


    Thanks a lot for your help for sorting the code for me! Massive help!

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Macro does not work with protected sheet

    Please Login or Register  to view this content.
    You're welcome. Always happy to try and help a fellow Aussie and thanks for rep

    This line of code...

    Please Login or Register  to view this content.
    ...loops through all the sheets in workbook.

    This line of code...

    Please Login or Register  to view this content.
    ...tells the code to ignore the code name of "Sheet2" as this tab is where the results are consolidated so it makes sense not to filter and copy its data. The code name of a sheet is found in the left-hand panel of the Visual Basic Editor (where the macros are housed). In your workbook's VBE you will see Sheet2 (Disposed) where Sheet2 is the code name and Disposed is the actual tab name you see from Excel's front end. The benefit of referencing sheets via their code name is that the tab name can change but the underlying code will still work. You can also change the code name of the tab if you wish via the VBE.

    This line of code...

    Please Login or Register  to view this content.
    ...sets the level of protection for the sheet. You see these options from the Protect Sheet dialog i.e. when you manually protect a sheet via the Protect Sheet option of the Protect tab from the Review ribbon. These are the default selections.

    Hope that helps.

    Robert
    Last edited by Trebor76; 07-24-2021 at 11:48 PM.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,035

    Re: Macro does not work with protected sheet

    @Kehong

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ 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. MAcro does not work when certain cells in a work sheet are protected
    By Unnati in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-10-2012, 03:38 AM
  2. Macro does not work when sheet is protected?? Please Help
    By savethisid in forum Excel General
    Replies: 1
    Last Post: 04-06-2012, 07:24 AM
  3. Macro does not work when sheet is protected??
    By savethisid in forum Excel General
    Replies: 1
    Last Post: 04-06-2012, 07:01 AM
  4. protected sheet, allow for macro to work
    By jw01 in forum Excel General
    Replies: 2
    Last Post: 08-22-2011, 02:55 PM
  5. Getting a Macro to work on protected sheet
    By Benw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2010, 06:33 AM
  6. Macro will not work when sheet is Protected
    By alachape in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-02-2008, 03:13 PM
  7. Replies: 2
    Last Post: 07-11-2006, 11:15 PM
  8. [SOLVED] Macro protected work sheet
    By Henry in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-14-2006, 07:40 PM

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