VBA Runtime Error : Clear Contents of an Excel sheet

logan picture logan · Dec 26, 2012 · Viewed 12.4k times · Source

I have written following VBA code in Excel 2003 to clear the contents of an excel sheet; But it shows

RunTime Error 438: Object doesn't Support this property or Method

Here is my code,

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Set wbk = Workbooks.Open("C:\a.xls")
wbk.Sheet1.Cells.ClearContents 'Error at this line
wbk.Save
wbk.Close

Application.DisplayAlerts = True
Application.ScreenUpdating = True

Could any one please let me know if wbk.Sheet1.Cells.ClearContents is not supported when excel is invisible. How can I clear content of an excel sheet when it is invisible?

Answer

logan picture logan · Dec 27, 2012

I have used the following and it is solved now!

wbk.Sheets("Sheet1").Cells.ClearContents