+ Reply to Thread
Results 1 to 15 of 15

Clear unprotected cells on all worksheets

  1. #1
    Forum Contributor
    Join Date
    07-19-2019
    Location
    Illinois, USA
    MS-Off Ver
    365
    Posts
    164

    Clear unprotected cells on all worksheets

    I'm using the following code that will clear all unprotected cells on the *CURRENT* worksheet.

    How can I modify this to clear all cells within specified worksheets? For example, if I wanted to clear on the following tabs only:

    NBA
    NFL
    MLB
    NHL


    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Clear unprotected cells on all worksheets

    Untested

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    07-19-2019
    Location
    Illinois, USA
    MS-Off Ver
    365
    Posts
    164

    Re: Clear unprotected cells on all worksheets

    It gives an error "1004"

    Method 'Union' of "object '_global' failed

  4. #4
    Forum Contributor
    Join Date
    07-19-2019
    Location
    Illinois, USA
    MS-Off Ver
    365
    Posts
    164

    Re: Clear unprotected cells on all worksheets

    But it actually worked on "NBA" - the first sheet -- the rest of them remain untouched?

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Clear unprotected cells on all worksheets

    It probably errors when the object is nothing. Just added a single line

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    07-19-2019
    Location
    Illinois, USA
    MS-Off Ver
    365
    Posts
    164

    Re: Clear unprotected cells on all worksheets

    Interesting -- all the sheets are cleared in half a second, but it keeps "thinking" for about 20 seconds until it errors out w/ the same error mentioned above.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    A VBA demonstration for starters :

    PHP Code: 
    Sub Demo1()
        
    Dim VRc As Range
            Application
    .ScreenUpdating False
        
    For Each V In [{"NBA","NFL","MLB","NHL"}]
        For 
    Each Rc In Sheets(V).UsedRange
            
    If Not (Rc.Locked Or Rc.HasFormulaThen Rc.ClearContents
        Next Rc
    V
            Application
    .ScreenUpdating True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  8. #8
    Forum Contributor
    Join Date
    07-19-2019
    Location
    Illinois, USA
    MS-Off Ver
    365
    Posts
    164

    Re: Clear unprotected cells on all worksheets

    Oops I spoke too soon - it's not clearing the other sheets like I thought -- it's still erroring out after sheet 1

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Clear unprotected cells on all worksheets

    I have only added two lines, otherwise it is your code.

    Please attach the sample and see what is going on

  10. #10
    Forum Contributor
    Join Date
    07-19-2019
    Location
    Illinois, USA
    MS-Off Ver
    365
    Posts
    164

    Re: Hi ! Try this !

    Marc L - yours seems like it's wanting to work right away - but it's erroring out because "we can't do that with a merged cell"

    Is there a workaround to work with merged cells?

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Clear unprotected cells on all worksheets


    Yes a workaround exists but we need your attachment : follow the top page yellow banner …

  12. #12
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Clear unprotected cells on all worksheets

    One way


    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  13. #13
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Clear unprotected cells on all worksheets

    For AB33's code you need to set rClear to Nothing after clearing it. If you have merged cells, I suggest using .Value = vbnullstring rather than .ClearContents. Well, actually I suggest not using merged cells, but that's another issue.
    Rory

  14. #14
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,361

    Re: Clear unprotected cells on all worksheets

    I was playing with this so i will share, my code is almost the same as AB33's so you can see where to empty the union range:

    Please Login or Register  to view this content.
    Could not agree more with rorya about avoiding merged cells, try using centre across selection in the formatting dialogue as an alternative.

  15. #15
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,386

    Re: Clear unprotected cells on all worksheets

    This method does not loop over every cell, and probably works with merged cells (but always avoid them)

    Please Login or Register  to view this content.

+ 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] Clear the contents of Unprotected cells - EXCEPT a certain range
    By ronkeakano in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-13-2015, 01:40 PM
  2. [SOLVED] Clear all unprotected cells
    By Drayde in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-25-2014, 04:58 PM
  3. [SOLVED] Clear all unprotected Cells
    By NicholasL in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2013, 07:53 PM
  4. Button to clear all unprotected cells including listboxes
    By davemojo82 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2009, 12:03 PM
  5. Replies: 3
    Last Post: 03-30-2006, 05:15 AM
  6. [SOLVED] clear contents cells of unprotected cells
    By Ed in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-12-2006, 02:15 PM
  7. [SOLVED] Clear contents of unprotected cells in entire workbook with a macr
    By FinnGirl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-29-2005, 01:05 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