Clearing Cells

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
Am having a problem clearing cells in my code, this has been working without any problem for months now, and am unsure what has changed to cause it to stop working.

Anyway here is the code

Code:
Sheets("Report").Select
Sheets("Report").Unprotect "password"
Sheets("Report").Range("A265:N624").Select
Range("A265:N624").Clear
Range("A265:N624").EntireRow.Hidden = True
Sheets("Report").Protect "password"
 For Each cl In Sheets("Report").Range("A1:N264").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

the error message I get is

"Run-time error '1004':

The cell or chart that you are trying to change is protected and therefore read-only.

To modify a protected cell or chart, first remove protection using the Unprotect Sheet command (Review tab, Changes group). You may be prompted for a password."

Any idea what might be causing this. Or how to find out what cell the error is occuring because of.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The error occured on the line

Code:
clMerge.ClearContents

I think I found the cause. 1 of the users had changed the cell properties of 1 of the merged cells to locked, when it should have been unlocked. Am unsure why this affected the code at that point though?

Have changed the cell back to unlocked and it seems to have fixed the problem though.
 
Upvote 0
how to find out what cell the error is occuring because of.



Code:
Sheets("Report").Select
Sheets("Report").Unprotect "password"
Sheets("Report").Range("A265:N624").Select
Range("A265:N624").Clear
Range("A265:N624").EntireRow.Hidden = True
Sheets("Report").Protect "password"
 For Each cl In Sheets("Report").Range("A1:N264").Cells
            [COLOR=Red]Debug.Print cl.Address ' VBA Immediate (debug window)
            'or cl.Interior.ColorIndex = 3 ' On screen fill color[/COLOR]
                            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

Gary
 
Upvote 0
Thanks, I've never used debug.print, that should come in handy with finding the problem quicker when the next one occurs.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,223
Members
452,896
Latest member
IGT

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