在 VBA 中将变量从表单传递到模块

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

Passing variable from Form to Module in VBA

excelvbamoduleuserform

提问by JimmyK

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:

然后我有一个名为 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 passfrom UserForm1 into Module1's Login sub.

这个想法是用户在输入框中输入的任何密码都将分配给变量pass。然而,我遇到的问题是pass从 UserForm1传递到 Module1 的 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.

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

回答by Siddharth Rout

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

不要在用户表单中声明变量。Public在模块中声明它。

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?

您可能还想在呼叫driver.find...线路之前添加额外的检查?

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

This will ensure that a blank string is not passed.

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

回答by Mathieu Guindon

Siddharth's answer is nice, but relies on globally-scoped variables. There's a better, more OOP-friendly way.

Siddharth 的回答很好,但依赖于全局范围的变量。有一种更好、更OOP 友好的方式。

A UserForm is a class module like any other - the only difference is that it has a hidden VB_PredeclaredIdattribute set to True, which makes VB create a global-scope object variable named after the class - that's how you can write UserForm1.Showwithout creating a new instance of the class.

UserForm 是一个类模块,与其他任何模块一样 - 唯一的区别是它的隐藏VB_PredeclaredId属性设置为True,这使得 VB 创建一个以该类命名的全局范围对象变量 - 这就是您可以在UserForm1.Show不创建新实例的情况下编写班级。

Step away from this, and treat your form as an object instead - expose Property Getmembers and abstract away the form's controls - the calling code doesn't care about controlsanyway:

远离这一点,将您的表单视为一个对象 - 公开Property Get成员并抽象出表单的控件 - 调用代码无论如何都不关心控件

Option Explicit
Private cancelling As Boolean

Public Property Get UserId() As String
    UserId = txtUserId.Text
End Property

Public Property Get Password() As String
    Password = txtPassword.Text
End Property

Public Property Get IsCancelled() As Boolean
    IsCancelled = cancelling
End Property

Private Sub OkButton_Click()
    Me.Hide
End Sub

Private Sub CancelButton_Click()
    cancelling = True
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        cancelling = True
        Me.Hide
    End If
End Sub

Now the calling code can do this (assuming the UserForm was named LoginPrompt):

现在调用代码可以做到这一点(假设 UserForm 被命名为LoginPrompt):

With New LoginPrompt
    .Show vbModal
    If .IsCancelled Then Exit Sub
    DoSomething .UserId, .Password
End With

Where DoSomethingwould be some procedure that requires the two string parameters:

DoSomething需要两个字符串参数的过程在哪里:

Private Sub DoSomething(ByVal uid As String, ByVal pwd As String)
    'work with the parameter values, regardless of where they came from
End Sub