VBA Form代码中的msgbox,从Module sub获取变量

VBA msgbox in Form code, get variable from Module sub

在 USER FORM 代码中,在用户点击 "OK," 后有一个消息框 IE。 MsgBox ("The total price is " & Price & "")

事实是,我已经计算了 MODULE 子中的 Price 变量。

如何确保实际显示价格值?如何将变量从子模块连接到表单?

更一般地说,我怎样才能在用户单击 "OK" 后显示一个消息框。我的代码中是否需要有两个消息框?

代码示例:

在模块中:

Dim Price As Currency

Sub Test1()
    Price = wsSheet.Range("A1").End(xlDown).Value
End Sub

形式:

Sub cmdOK_click
   MsgBox ("The total price is " & Price & "")
End Sub

All 表示您没有调用执行 Price 计算的 Sub。我建议你这样做:

在您的模块中:

Public Price As Currency

Sub Test1()

    Price = wsSheet.Range("A1").End(xlDown).Value

End Sub

在您的用户表单代码中:

Private Sub cmdOK_click()

    Call Test1
    MsgBox ("The total price is " & Price & "")

End Sub

如果您的代码计划允许,您也可以让 MsgBox 改为显示在模块中。所以你会有这样的东西:

在您的模块中:

Public Price As Currency

Sub Test1()

    Price = wsSheet.Range("A1").End(xlDown).Value
    MsgBox ("The total price is " & Price & "")

End Sub

在您的用户表单代码中:

Private Sub cmdOK_click()

    Call Test1

End Sub

请让我知道它是否对您有帮助。