在一个子集中设置变量,然后在另一个子集中使用 [英] Set a variable in one sub and use in another

查看:56
本文介绍了在一个子集中设置变量,然后在另一个子集中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA编程的新手,并且对此感到怀疑,这可能对您来说很简单.

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

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

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

我尝试使用全局变量,但对我没有用.谢谢

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

推荐答案

下面是一个示例,说明了如何在一个子变量中创建变量,然后在另一个子变量中使用变量:

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.

这篇关于在一个子集中设置变量,然后在另一个子集中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆