如何在VBA中更改方法名称的大小写?

时间:2014-07-08 14:55:46

标签: excel vba excel-2010

在学习VBA的过程中,我从互联网上做了大量的代码复制粘贴。在某些时候,我注意到每当我使用名为“Add”的方法时,VBA IDE会自动将其更改为“add”。例如,当我输入ActiveSheet.PivotTables.Add时,它会自动转换为ActiveSheet.PivotTables.addWorksheets.Add更改为Worksheets.add也是如此。如何恢复方法的默认情况?

我知道VBA IDE会根据声明的方式更改变量的大小写(请参阅:How does one restore default case to a variable in VBA (Excel 2010)?)。但我不确定它是否与我的问题有关。

1 个答案:

答案 0 :(得分:1)

这是因为你在项目中声明了一些变量或函数或子作为add(或者你可能已经加载了一些加载项)。

VBA环境将获取变量名称/函数名称的最后声明的case表示形式,并将它们应用于发生它们的每个实例。

E.g。

Function Func1()    
Dim ART As Double
ART = 1
' in one function
End Function

' Now if I type the following in another function
Function Func2()
Dim aRt as Double
End Function

' the original declaration in the first function becomes
Dim aRt As Double
aRt = 1

这种行为可能是理想的或不受欢迎的,但它绝对是设计的。

如果你想声明变量并且不希望它们改变大小写(对某些Enum等有用),你需要做类似的事情

#If False Then
   Dim HeWhoseCaseShallNotBeChanged
   Dim ADd     ' Even methods can be "declared" here. It will have no effect on execution
#End If

这适用于Enums等。您可以在模块页面的顶部声明此compiler directive