Pass data between UserForms

前端 未结 3 672
栀梦
栀梦 2020-12-14 14:06

Within Excel VBA I have a User Form similar to the following where the user enters an ID number and then the details are displayed on the user form:

Private          


        
3条回答
  •  死守一世寂寞
    2020-12-14 14:31

    There are many many ways... Here are some...

    Way 1

    1. Declare a Public Variable in a Module
    2. Assign to that variable in Userform1 and then launch Userform2. This variable will retain it's value. Example

    In Userform1

    Private Sub CommandButton1_Click()
        MyVal = "Sid"
        UserForm2.Show
    End Sub
    

    In Userform2

    Private Sub CommandButton1_Click()
        MsgBox MyVal
    End Sub
    

    In Module

    Public MyVal
    

    Way 2

    Use the .Tag property of the userform

    In Userform1

    Private Sub CommandButton1_Click()
        UserForm2.Tag = "Sid"
        UserForm2.Show
    End Sub
    

    In Userform2

    Private Sub CommandButton1_Click()
        MsgBox Me.Tag
    End Sub
    

    Way 3

    Add a Label in Userform2 and set it's visible property to False

    In Userform1

    Private Sub CommandButton1_Click()
        UserForm2.Label1.Caption = "Sid"
        UserForm2.Show
    End Sub
    

    In Userform2

    Private Sub CommandButton1_Click()
        MsgBox Label1.Caption
    End Sub
    

提交回复
热议问题