在VBA中将变量从Form转换为Module [英] Passing variable from Form to Module in VBA

查看:212
本文介绍了在VBA中将变量从Form转换为Module的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  Public Sub Login()

...

UserForm1.Show
driver.findElementByName(PASSWORD)SendKeys pass

...

End Sub

这个想法是用户输入到输入框中的任何密码将被分配给变量 pass 。然而,我遇到麻烦的是将 pass 从UserForm1传递到Module1的登录子。



我想在卸载之前,将...添加到我的表单中,如$ code Module1.Login(pass),但这似乎并没有传递任何东西。任何帮助将不胜感激。谢谢。

解决方案

不要在userform中声明变量。在模块中声明为 Public

 公共传递As String 

在Userform中



私人子命令Button1_Click()
pass = UserForm1.TextBox1
卸载UserForm1
End Sub
/ pre>

在模块

 公共传递As String 

公共子登录()
'
'~~>其余代码
'
UserForm1.Show
driver.findElementByName(PASSWORD)。SendKeys pass
'
'~~>其余代码
'
End Sub

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

 如果Len(Trim(pass))<> 0然后

这将确保未传递空白字符串。


I have the following button on a Form:

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

I then have a Module called Module1:

 Public Sub Login()

 ...

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

 ...

End 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.

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.

解决方案

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

Public pass As String

In the Userform

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

In the Module

Public pass As String

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

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中将变量从Form转换为Module的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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