vba 用户表单无法将值传递给模块

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

User form can't pass value to module

excel-vbavbaexcel

提问by Natalie

I've inserted a user form into a project that already contains a bunch of modules. By playing around with the code in the user form, I've verified that I can return the value from a combo box.

我已经将一个用户表单插入到一个已经包含一堆模块的项目中。通过使用用户表单中的代码,我验证了我可以从组合框中返回值。

User form code:

用户表单代码:

Public SelectedPacking As Integer

Private Sub CancelButton_Click()
    UserForm1.Hide        
 End Sub

Private Sub OKButton_Click()
    SelectedPacking = ComboBox1.ListIndex    
    Call DemoDialogOk

    'Return list index value to cell C50
    Worksheets("Inputs & Results").Range("C50") = SelectedPacking    
    Unload UserForm1    
End Sub

My problem is that I can't pass this value on to any of the macros written in the modules.

我的问题是我无法将此值传递给模块中编写的任何宏。

Module code:

模块代码:

Public Sub ShowComboBox()
    UserForm1.Show    
End Sub

 Public Sub DemoDialogOk()    
    ival = SelectedPacking

    'Return value of ival (list index value from combo box) to cell C17
     Worksheets("Packed bed (Random)").Range("C17") = ival
End Sub

Obviously the module contains more useful code, but I've commented out everything to try and figure out where I'm going wrong. I've been changing some things around, but I still can't get anything to appear in cell C17, so I think I'm missing something fundamental.

显然该模块包含更多有用的代码,但我已经注释掉了所有内容以试图找出我出错的地方。我一直在改变一些东西,但我仍然无法在单元格 C17 中出现任何东西,所以我想我错过了一些基本的东西。

采纳答案by Simon

I think two options: 1) change DemoDialogueOK to accept variables:

我认为有两个选择:1)更改 DemoDialogueOK 以接受变量:

Public Sub DemoDialogOk(SelPack as integer)    
    ' ival = SelectedPacking

    Worksheets("Packed bed (Random)").Range("C17") = SelPack
End Sub

Private Sub OKButton_Click()
    SelectedPacking = ComboBox1.ListIndex
    Call DemoDialogOk(SelectedPacking)

    ...
End Sub

Or option two: fully qualify the variable from the useform i.e:

或选项二:完全限定来自 useform 的变量,即:

Public Sub DemoDialogOk()
     ival = ufYourForm.SelectedPacking

    ...
End Sub

Public variables in userforms don't appear to be as "public" as module level...

用户表单中的公共变量似乎不像模块级别那样“公共”......

回答by SMJ

Tipping on top of Simon's answer, you could pass the entire userform if you'd like. This would give you access to all the pieces of it and is especially useful if you need to do some validation on, say, different checkboxes being checked or not.

在西蒙的回答之上,如果你愿意,你可以传递整个用户表单。这将使您可以访问它的所有部分,如果您需要对诸如是否选中不同复选框进行一些验证,这将特别有用。

 Sub inOurUserForm()
    Call inADifferentModule(Me) 'Passes this userform
 End Sub

 Sub inADifferentModule(ourForm As UserForm1)
    'Passed the form, and using it like a class (As whatever the form is called)
    If ourForm.chkYes = True Then
        'Do something
    Else
        'Do something else, like
        ourForm.chkYes = False 'Because we are passing the object itself _
                                 rather than a copy, at least in my understanding
    End If

End Sub

And you don't necessarily need to pass the userform, as you could just reference it as an object itselft e.g.

而且您不一定需要传递用户表单,因为您可以将其作为对象本身引用,例如

UserForm1.chkYes

回答by Vatsa

A very easy solution to this would be to declare a variable within the Userform (UserForm1 in this example)

一个非常简单的解决方案是在用户窗体中声明一个变量(在本例中为 UserForm1)

    Public Pass as string

This Pass would contain the string where you store the password. Once you store the password, you can hide the form

此 Pass 将包含您存储密码的字符串。存储密码后,您可以隐藏表单

    Me.Hide

Within the module, you can open the Form as modal

在模块内,您可以将表单作为模态打开

    UserForm1.Show vbModal

Now after all the code inside the userform is run, the password can be retrieved within the module -

现在,在用户表单中的所有代码运行后,可以在模块中检索密码 -

    UserForm1.Pass 

You can then unload the hidden form

然后您可以卸载隐藏的表单

    unload UserForm1