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!
 
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

Is it possible to have your code above insert a 0 (zero) instead of a blank when clearing? Am totally new to vba codes.
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is it possible to have a button in excel that insert a 0 (zero) instead of a blank when clearing unlocked cells in a protected sheet? Am totally new to vba codes.
 
Upvote 0
Hi @gkairu

Try this in a copy of your workbook as after running the Macro you won't get Undo option and I am a beginner in VBA.

VBA 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 = 0
    Next Cell
End Sub

P.S This code has been edited from @parry's Code
Hope this helps
 
Upvote 0
Hi @gkairu

Try this in a copy of your workbook as after running the Macro you won't get Undo option and I am a beginner in VBA.

VBA 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 = 0
    Next Cell
End Sub

P.S This code has been edited from @parry's Code
Hope this helps

Thanks, let me try it out.
 
Upvote 0
Another option
VBA Code:
Sub gkairu()
   With Application.FindFormat
      .Clear
      .Locked = False
   End With
   ActiveSheet.UsedRange.Replace "*", 0, , , , , True, False
   Application.FindFormat.Clear
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

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