Clear only unprotected cells

pilot

Active Member
Joined
Feb 17, 2002
Messages
345
I need a macro to clear unprotected cells on a protected sheet. If it matters, the cells may contain values or text. Also, this is in Excel2002 which offers lots more options with respect to sheet protection. I tried creating a named range that includes every cell in the print area but once sheet protection is activated, the entire range can't be selected. I would even accept clearing every unprotected cell on the sheet. I'll just bet there's some simple code to do this.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi there

You could try something like this:

For Each cell In Range("B4:J22")
If cell.Locked = False Then
cell.ClearContents
End If
Next
End Sub

Change the range to your range
(works in Excel 2000)

regards
Derek
 
Upvote 0
Hi
This will clear unlocked cells rather the
worksheet is protected or not...
Note:
If you do not want to wait around,
change this Line:

For Each c In ActiveSheet.Cells

to a bit smaller range such as:

For Each c In range.("A1:Z1000")

Sub ClearCells()
Dim c
For Each c In ActiveSheet.Cells
If c.Locked = False Then _
c.ClearContents
Next
End Sub


You may also Unprotect a sheet via code at
the beginning of you procedure,
run your code, and then protect it again.

ActiveSheet.Unprotect password:="YourPassword"
'code here
ActiveSheet.Protect password:="YourPassword"

The password is optional...

Tom
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,194
Members
448,554
Latest member
Gleisner2

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