VBA 函数和变量

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

VBA Functions and variables

excelfunctionvbavariables

提问by Jesse Smothermon

I have a question about variables and functions in VBA. I'm not going to include exactly what the project is in hopes that the initial question won't get to obscure. I can explain the project this will be used for if requested.

我有一个关于 VBA 中的变量和函数的问题。我不打算具体包括项目是什么,希望最初的问题不会变得模糊。如果需要,我可以解释这将用于的项目。

Is it possible to create universal variables in VBA so that all functions can use them?

是否可以在 VBA 中创建通用变量以便所有函数都可以使用它们?

Ex:

前任:

Dim testVariable As String
Dim newVariable As String

testVariable = "A"

Function testFunction() As String
    ....
    newVariable = testVariable
End Function

As of now, testVariable (when it is in the function) is "Empty"

截至目前,testVariable(当它在函数中时)是“Empty”

Thank you,

谢谢,

Jesse Smothermon

杰西·斯莫瑟蒙

采纳答案by mwolfe02

Yes, but the value assignments must occur within procedures:

是的,但值分配必须发生在过程中:

Public testVariable As String
Public newVariable As String

Sub SetTestVariable()
    testVariable = "A"
End Sub

Function testFunction() As String
    ....
    newVariable = testVariable
End Function

Keep in mind that it is generally bad programming practice to use a lot of global variables. You want to limit the scope of your variables to where they will be used. For more information about scope in VBA see this Microsoft KnowledgeBase article: Scope of variables in Visual Basic for Applications

请记住,使用大量全局变量通常是不好的编程习惯。您想将变量的范围限制在它们将被使用的地方。有关 VBA 中作用域的详细信息,请参阅此 Microsoft 知识库文章:Visual Basic for Applications 中的变量作用域

回答by RichardTheKiwi

Leave the variables out of any sub/function, so they become Globals

将变量排除在任何子/函数之外,因此它们成为全局变量

Then, call an init function before you run any code that contains the default settings, such as the code

然后,在运行任何包含默认设置的代码之前调用 init 函数,例如代码

Sub InitGlobalVars
    testVariable = "A"
End Sub

Sub MyProjectMain
    InitGlobalVars
    ....
    ... rest of code
End Sub

回答by Thomas

Is it possible to create universal variables in VBA so that all functions can use them?

是否可以在 VBA 中创建通用变量以便所有函数都可以使用它们?

Yes, although you need to declare the scope as stated by mwolf02. However, what you cannot do in VBA is populate them outside of a function or procedure call like you can with VBScript.

是的,尽管您需要按照 mwolf02 声明范围。但是,在 VBA 中不能做的是在函数或过程调用之外填充它们,就像使用 VBScript 一样。

In addition, if you really need global variables, you should derive a naming scheme that differentiates global variables from locally declared variables (e.g. prefix them with g_). You can get this confusing scenario:

另外,如果你真的需要全局变量,你应该派生出一个命名方案来区分全局变量和局部声明的变量(例如用 前缀g_)。你可以得到这个令人困惑的场景:

Public testVariable as String

Public Sub Main()
    Call Foo()
    Call Bar()
End Sub

Public Sub Foo()
    testVariable = "Foo"
End Sub

Public Sub Bar()
    Dim testVariable As String
    testVariable = "Bar"
End Sub

The value of the testVariablewill be "Foo" even though Barwas called later. In fact, there is no way to reference the global variable testVariablefrom within Bar since it has declared a local variable with the same name.

testVariable即使Bar稍后被调用,该值也会是“Foo” 。事实上,没有办法testVariable从 Bar 内部引用全局变量,因为它已经声明了一个同名的局部变量。

回答by Tiago Cardoso

Alternatively, you can use global constantsrather than variables.

或者,您可以使用全局常量而不是变量

Public Const TESTVARIABLE = "A"

I believe it fits better your question rather than setting the value within a specific function.

我相信它更适合您的问题,而不是在特定函数中设置值。

Obs1.: Global constants are (usually) stored in capital letters.

Obs1.:全局常量(通常)以大写字母存储。

Obs2.: You cannot set a globalconstant within a worksheet code. Use a specific module (global.bas, for instance) to do it.

Obs2.:您不能在工作表代码中设置全局常量。使用特定的模块(例如 global.bas)来完成。

Rgds

Rgds

回答by MP?kalski

Very brief example:

非常简短的例子:

Dim testVariable As String
Dim newVariable As String

Sub setVariable(ByVal x as string)
    testVariable = x
End Sub

Function testFunction() As String
    testFunction = testVariable
End Function

Sub test()
    Call setVariable("A")
    MsgBox testFunction()
    Call setVariable("B")
    MsgBox testFunction()
End Sub

As mentioned in other posts: globals are a bad practice, and a value has to be assigned inside sub/functions. They should also be defined at the beginning of the module.

正如其他帖子中提到的:全局变量是一种不好的做法,必须在子/函数中分配一个值。它们也应该在模块的开头定义。