Clear Cells

DavidC

New Member
Joined
Mar 17, 2002
Messages
26
I use the following code to clear the contents of cells that are not locked on a sheet so it can be reused easily:

Dim cl As Range
For Each cl In ActiveSheet.UsedRange
If Not cl.Locked Then cl.ClearContents
Next cl

However, it seems to take longer to run that it should. My data field is only about 25 columns by 125 rows. Is there a way to get it to go faster? Thanks.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Just timed it, and it takes about 40 seconds. And you know how people can complain. Thanks.
 
Upvote 0
It would go much faster if you limited the FOR/EACH loop so it didn't include all the formulas and blank cells in the UsedRange.

For Each cell In Cells.SpecialCells(xlConstants)
If Not cell.Locked Then cell.ClearContents
Next cell

You could take it a step further and add the extra constraint on the "SpecialCells(xlConstants, xlNumbers)" if you know you can further limit the scope by only looking for values.
 
Upvote 0
Uh oh, we got problems. Get an error that it could not be run on a protected sheet. Any way around that? Thanks.
 
Upvote 0
I almost forgot...

You might also try turning off screenupdating and set calculation to manual.

So... I guess the new code might look like:

Application.ScreenUpdating = False
Application.Calculation = xlManual
For Each cell In Cells.SpecialCells(xlConstants)
If Not cell.Locked Then cell.ClearContents
Next cell
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
 
Upvote 0
You should get in the habit of protecting the worksheets in code at workbook startup and use the "UserInterfaceOnly" option to allow your macros to run without sheet protection interference.

If you need an example, I've got one.
 
Upvote 0
Thanks, guys, I've been looking for a macro like this myself. Just a thought; this could be dangerous. How 'bout starting the macro with "ActiveWorkbook.Save" and, in the macro button, include the text "If problem occurs, close without saving and re-open."
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

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