在 VBA 中将变量从表单传递到模块 [英] Passing variable from Form to Module in VBA

查看:31
本文介绍了在 VBA 中将变量从表单传递到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有以下按钮:

I have the following button on a Form:

Private Sub CommandButton1_Click()
 Dim pass As String
 pass = UserForm1.TextBox1
 Unload UserForm1
End Sub

然后我有一个名为 Module1 的模块:

I then have a Module called Module1:

 Public Sub Login()

 ...

 UserForm1.Show
 driver.findElementByName("PASSWORD").SendKeys pass

 ...

End Sub

这个想法是用户在输入框中输入的任何密码都将分配给变量 pass.但是,我遇到的问题是将 pass 从 UserForm1 传递到 Module1 的 Login sub 中.

The idea is whatever password the users enters into the input box will be assigned to the variable pass. What I'm having trouble doing however is passing pass from UserForm1 into Module1's Login sub.

我想在我卸载它之前向我的表单添加诸如 Module1.Login (pass) 之类的东西会起作用,但是这似乎并没有传递任何东西.任何帮助将非常感激.谢谢.

I would of thought adding something like Module1.Login (pass) to my form before I unload it would work, however that doesn't seem to pass anything. Any help would be much appreciated. Thanks.

推荐答案

不要在用户表单中声明变量.在模块中将其声明为 Public.

Don't declare the variable in the userform. Declare it as Public in the module.

Public pass As String

在用户表单中

Private Sub CommandButton1_Click()
    pass = UserForm1.TextBox1
    Unload UserForm1
End Sub

在模块中

Public pass As String

Public Sub Login()
    '
    '~~> Rest of the code
    '
    UserForm1.Show
    driver.findElementByName("PASSWORD").SendKeys pass
    '
    '~~> Rest of the code
    '
End Sub

您可能还想在调用 driver.find... 行之前添加额外的检查?

You might want to also add an additional check just before calling the driver.find... line?

If Len(Trim(pass)) <> 0 Then

这将确保不传递空白字符串.

This will ensure that a blank string is not passed.

这篇关于在 VBA 中将变量从表单传递到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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