Excel Clear Cells :: MrExcel Message Board


 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

Clear Cells
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

DavidC
Board Regular


Joined: 18 Mar 2002
Posts: 27


Status: Offline

 Reply with quote  

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.

Post Wed Nov 06, 2002 6:01 pm 
 View user's profile Send private message

DavidC
Board Regular


Joined: 18 Mar 2002
Posts: 27


Status: Offline

 Reply with quote  

Just timed it, and it takes about 40 seconds. And you know how people can complain. Thanks.

Post Wed Nov 06, 2002 6:04 pm 
 View user's profile Send private message

Aaron Blood
Board Master


Joined: 11 Oct 2002
Posts: 145
Location: West Palm Beach, Florida

Status: Offline

 Reply with quote  

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.


Post Wed Nov 06, 2002 6:14 pm 
 View user's profile Send private message Send e-mail Visit poster's website

DavidC
Board Regular


Joined: 18 Mar 2002
Posts: 27


Status: Offline

 Reply with quote  

Down to 5 seconds. Thanks a lot!

Post Wed Nov 06, 2002 6:23 pm 
 View user's profile Send private message

DavidC
Board Regular


Joined: 18 Mar 2002
Posts: 27


Status: Offline

 Reply with quote  

Uh oh, we got problems. Get an error that it could not be run on a protected sheet. Any way around that? Thanks.

Post Wed Nov 06, 2002 6:27 pm 
 View user's profile Send private message

Aaron Blood
Board Master


Joined: 11 Oct 2002
Posts: 145
Location: West Palm Beach, Florida

Status: Offline

 Reply with quote  

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


Post Wed Nov 06, 2002 6:28 pm 
 View user's profile Send private message Send e-mail Visit poster's website

Aaron Blood
Board Master


Joined: 11 Oct 2002
Posts: 145
Location: West Palm Beach, Florida

Status: Offline

 Reply with quote  

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.

Post Wed Nov 06, 2002 6:31 pm 
 View user's profile Send private message Send e-mail Visit poster's website

DavidC
Board Regular


Joined: 18 Mar 2002
Posts: 27


Status: Offline

 Reply with quote  

An example would be great. You can email it to drcoon@hotmail_takethisout_.com. I.e., it is @hotmail.com. Just don't want my address to be picked up by some bot and added to a spam list. Thanks so much for your help.

Post Wed Nov 06, 2002 6:41 pm 
 View user's profile Send private message

Barry Katcher
Board Master
Board  Master


Joined: 26 Feb 2002
Posts: 1930
Location: Florida. Yeaahh!
Flag: Usa

Status: Offline

 Reply with quote  

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."

_________________
Barry-I used to be a werewolf, but I'm much better nowWWWWWWW!

Post Wed Nov 06, 2002 6:58 pm 
 View user's profile Send private message Send e-mail AIM Address

Aaron Blood
Board Master


Joined: 11 Oct 2002
Posts: 145
Location: West Palm Beach, Florida

Status: Offline

 Reply with quote  

David,

You and anyone else who may be interested can get the example file straight from my website.

Look for the "user_interface_only.zip" file in the VBA section.

Regards,
Aaron

http://www.XL-Logic.com

Post Wed Nov 06, 2002 8:04 pm 
 View user's profile Send private message Send e-mail Visit poster's website

DavidC
Board Regular


Joined: 18 Mar 2002
Posts: 27


Status: Offline

 Reply with quote  

Worked like a charm! Thanks!

Post Wed Nov 06, 2002 8:58 pm 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.