Excel VBA - Range.Clear 不清除单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16805045/
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
Excel VBA - Range.Clear does not clear cell
提问by Dulini Atapattu
I have the following function in my VBA which tries to clear the cell A1 in Excel Sheet1, but it does not clear the contents and also returns False as output:
我的 VBA 中有以下函数,它试图清除 Excel Sheet1 中的单元格 A1,但它没有清除内容并且还返回 False 作为输出:
Function test()
Application.Volatile (False)
test = Worksheets("Sheet1").Range("A1:A1").Clear
End Function
Also please not the Locked property is True at this time and even if I make it False, the next line if I print it, the Locked property is True.
另外请不要此时 Locked 属性为 True,即使我将其设为 False,如果我打印下一行,Locked 属性也是 True。
Any suggestions?
有什么建议?
Thanks
谢谢
回答by Jüri Ruut
- Function in VBA is generally a variable having a data type. In your example, data type is missing, meaning that the data type is set to
Variable
. - Apparently, your data type happens to be boolean, so a function is set to
False
. It is a bad style to use functions for changing cell contents. However, your code works without problems as a Sub:
Sub test() Application.Volatile (False) Worksheets("Sheet1").Range("A1:A1").Clear End Sub
- VBA 中的函数通常是具有数据类型的变量。在您的示例中,缺少数据类型,这意味着数据类型设置为
Variable
。 - 显然,您的数据类型恰好是布尔值,因此函数设置为
False
. 使用函数更改单元格内容是一种糟糕的风格。但是,您的代码作为 Sub 可以正常工作:
Sub test() Application.Volatile (False) Worksheets("Sheet1").Range("A1:A1").Clear End Sub
回答by KV Ramana
I am not sure exactly but as far as i know you cannot format excel sheet in Function instead we can do it using Subroutine.
我不确定,但据我所知,您无法在 Function 中格式化 Excel 工作表,而我们可以使用 Subroutine 来完成。
Sub test()
Worksheets("sheet1").Range("A1").Clear
End Sub