vba 在一个子程序中设置一个变量并在另一个子程序中使用

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

Set a variable in one sub and use in another

excelvbaexcel-vba

提问by VikkyB

I am new to VBA programming and had a doubt which may be quite simple for you.

我是 VBA 编程的新手,有一个疑问,这对您来说可能很简单。

How do we set a variable in one sub which can be used in another?

我们如何在一个 sub 中设置一个可以在另一个 sub 中使用的变量?

I tried using global variable but it didnt work for me. Thank you

我尝试使用全局变量,但它对我不起作用。谢谢

回答by Mardin Yadegar

Here is an example of how I have created a variable in one sub and used it in another:

这是我如何在一个子程序中创建变量并在另一个子程序中使用它的示例:

    Private Sub txtLastName_LostFocus()
    FirstName = Me.txtFirstName.Value
    LastName = Me.txtLastName.Value
    FullName = FirstName & " " & LastName
    sayHelloToTheUser (FullName)
    End Sub

    Private Sub sayHelloToTheUser(name As String)
    MsgBox "Hello " & name
    End Sub

Essentially, you must pass it through using another sub and having it take the arguments that are necessary. This is the main way that I pass arguments through.

本质上,您必须使用另一个 sub 传递它并让它接受必要的参数。这是我传递参数的主要方式。