VBA 运行时错误:清除 Excel 工作表的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14038595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:58:34  来源:igfitidea点击:

VBA Runtime Error : Clear Contents of an Excel sheet

excelvbaexcel-vbaexcel-2003

提问by logan

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

我在 Excel 2003 中编写了以下 VBA 代码来清除 Excel 工作表的内容;但它显示

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

运行时错误 438:对象不支持此属性或方法

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.ClearContentsis not supported when excel is invisible. How can I clear content of an excel sheet when it is invisible?

任何人都可以让我知道当 excel 不可见时是否不支持wbk.Sheet1.Cells.ClearContents。当excel表不可见时,如何清除它的内容?

回答by logan

I have used the following and it is solved now!

我已经使用了以下方法,现在已经解决了!

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