Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing Used cells in Excel? 2

Status
Not open for further replies.

BChumie

Programmer
Jan 11, 2002
130
US
Hello,

Is there any way to clear out all of the data from an excel spreadsheet using VBA. When I ask this question currently I am clearing a range of cells, but in order to clear the entire range of cells I have a HUGE range.

workbooks("book1.xls").worksheets(1).range("A1:ag65000").clear

I was wondering if there was a way to clear just the cells that have been used (there is no set number that is used, it varies from month to month). So if one month I use the range ("A1:AG3000") and the next month I use the range ("A1:AG60000") is there a way to clear out just the range i used for that month. Any help would be greatly appreciated.

Thanks,
Brad
 
If there are no cells containing data that you need to keep, just clear the entire sheet rather then worry about ranges.

Sheets("Sheet1").Cells.ClearContents

 
Or you could try:

Sheets("Sheet1").UsedRange.Cells.clearcontents

you may need to add
colCount = sheets("Sheet1").usedrange.columns.count
rowCount = sheets("Sheet1").usedrange.rows.count

before the clearcontents statement just to make sure that the usedrange is accurate

HTH
Geoff
 
Thanks to both of you for this information.

-Brad
 
This one still freaks me out coz I just don't understand what I've done!!

However the code appears to select cells containing data but not formulae so it may be useful for clearing data. It also appears to select a true used range. Anyone who wants to test it fully is more than welcome!!!

Sub used_Range_ofsorts()
With ActiveSheet.Range("A1").SpecialCells(xlEnd)
.Select
End With
' do something here eg selection.clearcontents
' though obviously it isn't necessary to ".Select"
End Sub

;-) If a man says something and there are no women there to hear him, is he still wrong?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top