Clearing all values in range that aren't formulas

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a range called myRange and I'd like to clear out all values that
aren't formulas. How would I do this programmatically?

Thanks
 
Sub ClearNonFormulas()
Dim cl As Range, myRange As Range
Set myRange = Range("myRange")
For Each cl In myRange.Cells
If cl.HasFormula = False Then cl.ClearContents
Next cl
End Sub

HTH
-John Coleman
 
Bard, one way,
Range("myrange").SpecialCells(xlConstants).ClearContents

--
Paul B
Always backup your data before trying something new
Please post any response to the newsgroups so others can benefit from it
Feedback on answers is always appreciated!
Using Excel 2002 & 2003
 
You are being modest, you should almost say "the only way". I created a
range with about 200,000 cells and my looping-through-the cells
checking the hasformula property strategy took several minutes but the
shorter code you gave works in about 10 seconds. Pretty nice speed-up!

Thanks for educating me a bit.

-John Coleman
 
Thanks. That does exactly what I want.

John Coleman said:
Sub ClearNonFormulas()
Dim cl As Range, myRange As Range
Set myRange = Range("myRange")
For Each cl In myRange.Cells
If cl.HasFormula = False Then cl.ClearContents
Next cl
End Sub

HTH
-John Coleman
 
Tom, thanks I did not know about that.

--
Paul B
Always backup your data before trying something new
Please post any response to the newsgroups so others can benefit from it
Feedback on answers is always appreciated!
Using Excel 2002 & 2003
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top