Pass data between UserForms

前端 未结 3 667
栀梦
栀梦 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:29

    The most simplest way is:

    UserForm2.TxtIDNo.Text = UserForm1.txtIDNo.Text

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-14 14:36

    There are serveral ways to solve this problem. The one that I use is declare global or public variable in module

    Example:

    Public commonVariable As String
    

    then in userform you can assign or retrieve value from this variable. For example in userform1:

    Private Sub btnIDNo_Click()
        commonVariable = "UserId"
    End Sub
    

    in UserForm2:

    Private Sub CommandButton1_Click()
        me.txtIDNo.Text = commonVariable 
    End Sub
    
    0 讨论(0)
提交回复
热议问题