+ Reply to Thread
Results 1 to 12 of 12

Clear the contents of Unprotected cells - EXCEPT a certain range

  1. #1
    Registered User
    Join Date
    11-06-2015
    Location
    Charleston
    MS-Off Ver
    2013
    Posts
    10

    Exclamation Clear the contents of Unprotected cells - EXCEPT a certain range

    Hi guys,

    I'm looking to create a Macro I can assign to button that will clear the contents of all Unprotected cells in my sheet EXCEPT a specific range of cells (that are also unprotected).

    The worksheet is filled out daily, so most of it needs to be cleared and re-entered. However there are a few blocks of cells that change so infrequently that the it isn't efficient for the end user to have to re-enter daily, but they still need access to change them when necessary.

    I am using the below code which takes care of the clearing all the unprotected cells. I'm hoping there is an amendment that can be made for it to EXCLUDE the block of cells I'm referring to.

    Please Login or Register  to view this content.

    Thanks!
    Last edited by Leith Ross; 11-06-2015 at 05:16 PM.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    Theoretically,you could exclude the intersection of your special ranges and the range returned by the FindFormat.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    11-06-2015
    Location
    Charleston
    MS-Off Ver
    2013
    Posts
    10

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    Could you elaborate? I am a VBA novice.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    What are your special ranges?

  5. #5
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    423

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    One way of doing this without having to hard code the specific range of cells in VBA is to use formatting - i.e. give the cells you don't want cleared a different background colour.

    The code below will clear all unlocked cells that don't have a pale blue background colour.

    Please Login or Register  to view this content.
    To chose your own colour, in Excel select a cell and chose the background colour you want. Then, in the VBA Immediate Window (View -> Immediate Window) type
    Please Login or Register  to view this content.
    It will return a number that you can use for SkipColour.
    Last edited by mgs73; 11-11-2015 at 12:32 AM.

  6. #6
    Registered User
    Join Date
    11-06-2015
    Location
    Charleston
    MS-Off Ver
    2013
    Posts
    10

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    Thank you mgs73 for the reply. I was able to find the number for the color of the cells, but get a compile error when entering the code below.


    Public Const SkipColour As Long = 13158083

    Public Sub Test()
    ClearCells ActiveSheet
    End Sub

    Public Sub ClearCells(sh As Worksheet)
    Dim Cell As Range

    For Each Cell In sh.UsedRange
    If Not Cell.Locked And Not Cell.Interior.Color = SkipColour Then Cell.ClearContents
    Next
    End Sub



    The error states that "constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules."

    Any idea as to what went wrong?
    Last edited by ronkeakano; 11-11-2015 at 10:04 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    423

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    You must have the code in a worksheet code module or in ThisWorkbook, where you cannot declare a Public Const.

    You can either copy all of the code into a standard module, or simply replace SkipColour in the code with the number of your colour, and remove Public Skip Colour etc... at the top.

  8. #8
    Registered User
    Join Date
    11-06-2015
    Location
    Charleston
    MS-Off Ver
    2013
    Posts
    10

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    Man o man...I'm not having much luck with this. Looks like this code cannot be used for Merged Cells. Is there any way to get around this that doesn't include unmerging the cells?

  9. #9
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    423

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    Try this:

    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    (I did not think this would work - but it did, at least for me... Good luck!)

  10. #10
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    I use the following checks to deal with merged areas

    Please Login or Register  to view this content.
    A blue font is my check for unprotected cells

  11. #11
    Registered User
    Join Date
    11-06-2015
    Location
    Charleston
    MS-Off Ver
    2013
    Posts
    10

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    Now how do i marry all these ideas into one that actually works?

  12. #12
    Registered User
    Join Date
    11-06-2015
    Location
    Charleston
    MS-Off Ver
    2013
    Posts
    10

    Re: Clear the contents of Unprotected cells - EXCEPT a certain range

    Just for future reference, I've figured it out.

    If you are looking to clear the contents from all unprotected cells (merged cells included) with the exception of a select few cells, the below code works Beautifully!

    Please Login or Register  to view this content.

    Where 13158083 can be replaced with the cell color of choice.

    To copy what mgs73 posted...

    To chose your own colour, in Excel select a cell and chose the background colour you want. Then, in the VBA Immediate Window (View -> Immediate Window) type

    Please Login or Register  to view this content.

    Thanks for all who helped out. I hope this can help others too.
    Last edited by ronkeakano; 11-13-2015 at 01:46 PM.

+ 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 all unprotected cells
    By Drayde in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-25-2014, 04:58 PM
  2. [SOLVED] Clear all unprotected Cells
    By NicholasL in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2013, 07:53 PM
  3. Clear contents of cells in a range, LEN=0
    By jenny_journalist in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-29-2009, 11:37 AM
  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. 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. 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