访问用户窗体Initialize子中ThisWorkbook中声明的变量

时间:2019-07-16 08:36:05

标签: excel vba

我正在基于1到5的用户输入来设置用户窗体,但是,我似乎无法将此输入输入到Userform_Initialise子窗体中。

Public TP As String 'this is in ThisWorkbook; from here

Private Sub Workbook_Open()

Dim RT As String

ReEnter:

    RT = Application.InputBox("1 - Monthly" & vbNewLine & "2 - Quarterly" & vbNewLine & "3 - Semi-Annually" & _
    vbNewLine & "4 - Semi-Annually" & vbNewLine & "5 - Others", "Type of 
    Report", "Enter Number Here", Type:=1)
    If RT >= 1 And RT <= 5 Then
        TP = Val(RT)
    Else
        MsgBox "Error", vbCritical
        GoTo ReEnter

    End If

UserForm1.Show

End Sub 'to here

'This onwards is in Userform
Private Sub Userform_Initialize()

Debug.Print TP

End Sub

如何获取用于识别用户表单中用户输入TP的代码?预先感谢!

1 个答案:

答案 0 :(得分:2)

在模块中声明全局变量

Public TP As String 'this should be in a module

,或者如果它在ThisWorkbook中声明,则必须使用

Debug.Print ThisWorkbook.TP

在您的用户窗体中。

相关问题