Pass data between UserForms

旧时模样 提交于 2019-11-30 04:04:07

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

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

The most simplest way is:

UserForm2.TxtIDNo.Text = UserForm1.txtIDNo.Text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!