Clear unlocked cells

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
Probably doing something really obvious wrong, but all I want to do is clear the contents of all unlocked cells in a worksheet when the worksheet is activated

here is the code I have which is giving me an error

Code:
Private Sub Worksheet_Activate()
Sheets("Sheet1").Unprotect "password"
Dim r   As Range
Set r = Sheets("Sheet1").Range("E3:K25")
For Each Cell In r
If Cell.Locked = False Then
Cell.ClearContents
End If
Next
Sheets("Sheet1").Protect "password"
End Sub

Where am I going wrong?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
It's probably not this but just a little minor stuff:
Dim cell As Range.
Use Worksheets("Sheet1") instead of Sheets("Sheet1")
 
Upvote 0
Application Defined or Object Defined error.

The sheet name is definitely correct.

Just realised that the reason why is because I am using merged cells, which always complicate things.

Any suggestions on how to deal with this,

uses .mergearea or something doesn't it?
 
Upvote 0
Got it going

Code:
Worksheets("Sheet1").Unprotect "password"
Dim cl As Range
 For Each cl In Sheets("Sheet1").Range("E3:K25").Cells
                            If cl.Locked = False Then
                                If cl.MergeCells = True Then
                                    Set clMerge = Range(cl.Address).MergeArea
                                    clMerge.ClearContents
                                Else
                                    cl.ClearContents
                                End If
                            End If
                                Next cl
Worksheets("Sheet1").Protect "password"

Thanks for the help
 
Last edited:
Upvote 0
Oic.
Forgot we had to use .Cells for looping xD

Thanks for coming back and putting up the working code.
 
Upvote 0
I try to always post up the working code just in case somebody has a similar problem. I know how many I have been able to solve just by googling and then adapting somebody elses solution to match my criteria.

Plus people are helping for free, so it's important to thank them.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

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