Clear all unlocked cells (protected sheets)

keessieb

Board Regular
Joined
Mar 5, 2003
Messages
177
In a workbook with several protected sheets, I would like to give an option for the user to clear all not protected cells. Just a button on sheet one what says "clear all". Can it be done? (all not protected cells are yellow, is there a way to say "clear all yellow cells?)"

Thanks in advance!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Heres a version specific to your requirements. Note this only works on the current active sheet (not all sheets). If you dont know how to add a button or add the code post again :)

Code:
Sub ClearUnlockedCells()
    Dim WorkRange As Range
    Dim Cell As Range
    Set WorkRange = ActiveSheet.UsedRange
    For Each Cell In WorkRange
        If Cell.Locked = False Then Cell.ClearContents
    Next Cell
End Sub

hth
 
Upvote 0
And another question: to use the same code for all worksheets, do I have to put the code in every sheet or can I use the code from one place? (Every sheet a button, button get's the code from one place)
 
Upvote 0
mmm interesting that clearcontents doesnt work with merged cells. Nevermind, heres an alternative...

Clear contents in just the active sheet
Code:
Sub ClearUnlockedCells()
    Dim WorkRange As Range
    Dim Cell As Range
    Set WorkRange = ActiveSheet.UsedRange
    For Each Cell In WorkRange
        If Cell.Locked = False Then Cell.Value = ""
    Next Cell
End Sub


Clear contents in all sheets
Code:
Sub ClearUnlockedAllSheets()
Dim Cell As Range, Sht As Worksheet
  
For Each Sht In Worksheets
    For Each Cell In Sht.UsedRange
    If Cell.Locked = False Then Cell.Value = ""
    Next Cell
Next Sht
End Sub
 
Upvote 0
This works great! I just need to add 1 thing to this which I have on another workbook. I need to advance the Date of cell H1 +7 days on the first sheet only. Below is the line that I stuck in another macro that I have but I tried to put it in this one and it doesn't work the same.


Worksheets("KENNETH-HUBERT").Range("H1").Value = Worksheets("KENNETH-HUBERT").Range("H1").Value + 7
 
Upvote 0
mmm interesting that clearcontents doesnt work with merged cells. Nevermind, heres an alternative...

Clear contents in just the active sheet
Code:
Sub ClearUnlockedCells()
    Dim WorkRange As Range
    Dim Cell As Range
    Set WorkRange = ActiveSheet.UsedRange
    For Each Cell In WorkRange
        If Cell.Locked = False Then Cell.Value = ""
    Next Cell
End Sub


Clear contents in all sheets
Code:
Sub ClearUnlockedAllSheets()
Dim Cell As Range, Sht As Worksheet
 
For Each Sht In Worksheets
    For Each Cell In Sht.UsedRange
    If Cell.Locked = False Then Cell.Value = ""
    Next Cell
Next Sht
End Sub
Is it possible to change the values in the unlocked cells to 0(zero) when the button is clicked?
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top