VBA如何将用户窗体变量传递给子例程 [英] VBA how to pass userform variables to subroutine

查看:781
本文介绍了VBA如何将用户窗体变量传递给子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用子例程从用户窗体输入中调用变量. 但是,即使我可以通过将用户窗体输入打印到工作表上来正确显示用户窗体输入,但似乎无法将其作为公共变量传递给子例程.当我在子例程中调用变量时,它们都是空的.

I am trying to use a subroutine call a variable from a userform input. But, even though I can display the userform inputs correctly in the userform by printing it to the sheet, I can't seem to pass it as a public variable to the subroutine. When I call the variables in the subroutine, they are all empty.

我以此为指导: 将变量从表单传递到VBA中的模块

这是我的代码:

Public a As String
Public b As Double
Public c As String

Private Sub OKButton_Click()
'from userform
    a = TextBox1.Value
    b = TextBox2.Value
    c = TextBox3.Value

    Cells(1, 1).Value = a
    Cells(2, 1).Value = b
    Cells(3, 1).Value = c
    'this displays the inputs properly
    Unload UserForm1
End Sub

在模块中:

Public Sub Login()
'in module
    UserForm1.Show
    MsgBox (a)
    MsgBox (b)
    MsgBox (c)

End Sub

推荐答案

这样做.将您的公共变量放入模块代码中.

Do like this. Put your public variable in the module code.

Public a As String
Public b As Double
Public c As String

Public Sub Login()
'in module
    UserForm1.Show
    MsgBox (a)
    MsgBox (b)
    MsgBox (c)

End Sub

这篇关于VBA如何将用户窗体变量传递给子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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