vba Excel 2007 VBA中Application.sum和Application.Sum的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21570462/
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-12 01:45:00  来源:igfitidea点击:

Difference between Application.sum and Application.Sum in Excel 2007 VBA

excelvbaexcel-2007

提问by Desmond27

I am writing a macro in VBA to get the sum of the values of a range in Excel 2007. However, whenever I try to type the following code :

我正在 VBA 中编写一个宏来获取 Excel 2007 中某个范围的值的总和。但是,每当我尝试键入以下代码时:

sum = Application.Sum(Range("C2:C19")

The editor turns it into :

编辑器把它变成:

sum = Application.sum(Range("C2:C19")

Since I was not sure of the difference between them I tried them out in the Immediate pane with the Print statement. The one with Application.sum gives me the wrong sum while the one with Application.Sum gives me the correct sum. But since the editor automatically changes Sum to sum, I am unable to proceed further. Can anyone please tell me what's going on over here?

由于我不确定它们之间的区别,因此我在“立即”窗格中使用 Print 语句尝试了它们。带有 Application.sum 的那个给了我错误的总和,而带有 Application.Sum 的那个给了我正确的总和。但由于编辑器自动将 Sum 更改为 sum,我无法进一步进行。谁能告诉我这里发生了什么?

回答by phrebh

The underlying question of why the VBIDE changes "Sum" to "sum" has to do with the editor's auto-capitalization functionality. You declared a variable as "sum", which takes precedence over the built-in function when it comes to capitalization. If you declared your variable as "Sum", then you wouldn't see the change. In reality, Application.Sum and Application.sum are the same.

为什么 VBIDE 将“Sum”更改为“sum”的根本问题与编辑器的自动大写功能有关。您将变量声明为“sum”,它在大小写方面优先于内置函数。如果您将变量声明为“Sum”,那么您将看不到更改。实际上,Application.Sum 和 Application.sum 是一样的。

The issue you were running into was that you were using an existing function as a variable name. That's why it wouldn't work.

您遇到的问题是您使用现有函数作为变量名。这就是它行不通的原因。