VBA Excel 上“ClearContents”的 2 种方法,但 1 种工作正常。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18962890/
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
2 ways for "ClearContents" on VBA Excel, but 1 work fine. Why?
提问by Asrhael
Good evening friends:
朋友们晚上好:
I have in mind 2 ways for clearing a content in a defined range of cells of a VBA project (in MS Excel):
我想到了两种清除 VBA 项目(在 MS Excel 中)的定义单元格范围中的内容的方法:
Worksheets("SheetName").Range("A1:B10").ClearContents
Worksheets("SheetName").Range(Cells(1, 1), Cells(10, 2)).ClearContents
Worksheets("SheetName").Range("A1:B10").ClearContents
Worksheets("SheetName").Range(Cells(1, 1), Cells(10, 2)).ClearContents
The problem is that the second way show me an error '1004' when I'm not watching the current Worksheet "SheetName" (in other words, when I haven't "SheetName" as ActiveSheet).
问题是,当我没有观看当前工作表“SheetName”(换句话说,当我没有将“SheetName”作为 ActiveSheet)时,第二种方式会向我显示错误“ 1004”。
The first way work flawlessly in any situation.
第一种方式在任何情况下都能完美运行。
Why does this happen? How can I use the "Second way" without this bug?
为什么会发生这种情况?如何在没有此错误的情况下使用“第二种方式”?
回答by tigeravatar
It is because you haven't qualified Cells(1, 1)
with a worksheet object, and the same holds true for Cells(10, 2)
. For the code to work, it should look something like this:
这是因为您没有Cells(1, 1)
使用工作表对象进行限定,对于Cells(10, 2)
. 要使代码正常工作,它应该如下所示:
Dim ws As Worksheet
Set ws = Sheets("SheetName")
Range(ws.Cells(1, 1), ws.Cells(10, 2)).ClearContents
Alternately:
交替:
With Sheets("SheetName")
Range(.Cells(1, 1), .Cells(10, 2)).ClearContents
End With
EDIT: The Range object will inherit the worksheet from the Cells objects when the code is run from a standard module or userform. If you are running the code from a worksheet code module, you will need to qualify Range
also, like so:
编辑:当代码从标准模块或用户窗体运行时,范围对象将从单元格对象继承工作表。如果您从工作表代码模块运行代码,则还需要进行限定Range
,如下所示:
ws.Range(ws.Cells(1, 1), ws.Cells(10, 2)).ClearContents
or
或者
With Sheets("SheetName")
.Range(.Cells(1, 1), .Cells(10, 2)).ClearContents
End With
回答by Siddharth Rout
That is because you are not fully qualifying your cells object. Try this
那是因为您没有完全限定您的单元格对象。尝试这个
With Worksheets("SheetName")
.Range(.Cells(1, 1), .Cells(10, 2)).ClearContents
End With
Notice the DOT before Cells?
注意到 Cell 之前的 DOT 了吗?
回答by certifiedtranslator.hu
For numerical addressing of cells try to enable S1O1 checkbox in MS Excel settings. It is the second tab from top (i.e. Formulas), somewhere mid-page in my Hungarian version.
对于单元格的数字寻址,请尝试在 MS Excel 设置中启用 S1O1 复选框。它是顶部的第二个选项卡(即公式),位于我的匈牙利语版本的中间页面。
If enabled, it handles VBA addressing in both styles, i.e. Range("A1:B10") and Range(Cells(1, 1), Cells(10, 2)). I assume it handles Range("A1:B10") style only, if not enabled.
如果启用,它将处理两种风格的 VBA 寻址,即 Range("A1:B10") 和 Range(Cells(1, 1), Cells(10, 2))。如果未启用,我假设它仅处理 Range("A1:B10") 样式。
Good luck!
祝你好运!
(Note, that Range("A1:B10") represents a 2x10 square, while Range(Cells(1, 1), Cells(10, 2)) represents 10x2. Using column numbers instead of letters will not affect the order of addresing.)
(注意,Range("A1:B10") 代表一个 2x10 的正方形,而 Range(Cells(1, 1), Cells(10, 2)) 代表 10x2。使用列号代替字母不会影响地址顺序.)