vba 在用户窗体之间传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32520558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass data between UserForms
提问by Ben Smith
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:
在 Excel VBA 中,我有一个类似于以下内容的用户表单,其中用户输入 ID 号,然后详细信息显示在用户表单上:
Private Sub btnIDNo_Click()
Dim IDNo As Long
If txtIDNo.Text <> "" Then
If IsNumeric(txtIDNo.Text) = True Then
lblError.Caption = ""
IDNo = txtIDNo.Text
Worksheets("Details").Activate
Range("B4").Select
While ActiveCell.Value <> "" And ActiveCell.Value <> IDNo
ActiveCell.Offset(1, 0).Select
Wend
If ActiveCell.Value = IDNo Then
txtName.Value = ActiveCell.Offset(0, 1).Value
txtPhone.Value = ActiveCell.Offset(0, 2).Value
Else
lblError.Caption = "Cannot find ID nummber"
End If
Else
lblError.Caption = "Please enter the ID Number in numeric form"
End If
End If
End Sub
On the Details User Form, I have an "Edit" button. Clicking the "Edit" button would open another user form where the user can change the details of that ID number, but obviously not the ID number itself. To do this, I need to pass the ID number from the Details User Form to the Edit User Form. Is there a way of doing this?
在详细信息用户表单上,我有一个“编辑”按钮。单击“编辑”按钮将打开另一个用户表单,用户可以在其中更改该 ID 号的详细信息,但显然不能更改 ID 号本身。为此,我需要将 ID 号从详细信息用户表单传递到编辑用户表单。有没有办法做到这一点?
The bottom on the Show Details User Form to open the Edit User Form is similar to the following:
在 Show Details User Form 的底部打开 Edit User Form 类似于以下内容:
Private Sub CommandButton1_Click()
Dim IDNo As Long
If txtIDNo.Text <> "" Then
If IsNumeric(txtIDNo.Text) = True Then
lblError.Caption = ""
IDNo= txtIDNo.Text
ufmEditDetails.Show
ufmShowDetails.Hide
Else
lblError.Caption = "Please enter the ID Number in numeric form"
End If
Range("B4").Select
End If
End Sub
I have already looked at the following links but they don't seem to help:
我已经查看了以下链接,但它们似乎没有帮助:
http://gregmaxey.mvps.org/word_tip_pages/userform_pass_data.html
http://gregmaxey.mvps.org/word_tip_pages/userform_pass_data.html
回答by Siddharth Rout
There are many many ways... Here are some...
方法很多……这里有一些……
Way 1
方式一
- Declare a
PublicVariable in a Module - Assign to that variable in Userform1 and then launch Userform2. This variable will retain it's value. Example
Public在模块中声明变量- 在 Userform1 中分配给该变量,然后启动 Userform2。此变量将保留其值。例子
In Userform1
在用户表单 1 中
Private Sub CommandButton1_Click()
MyVal = "Sid"
UserForm2.Show
End Sub
In Userform2
在 Userform2 中
Private Sub CommandButton1_Click()
MsgBox MyVal
End Sub
In Module
模块内
Public MyVal
Way 2
方式二
Use the .Tagproperty of the userform
使用用户.Tag表单的属性
In Userform1
在用户表单 1 中
Private Sub CommandButton1_Click()
UserForm2.Tag = "Sid"
UserForm2.Show
End Sub
In Userform2
在 Userform2 中
Private Sub CommandButton1_Click()
MsgBox Me.Tag
End Sub
Way 3
方式三
Add a Labelin Userform2 and set it's visible property to False
Label在 Userform2 中添加一个并将其可见属性设置为False
In Userform1
在用户表单 1 中
Private Sub CommandButton1_Click()
UserForm2.Label1.Caption = "Sid"
UserForm2.Show
End Sub
In Userform2
在 Userform2 中
Private Sub CommandButton1_Click()
MsgBox Label1.Caption
End Sub
回答by Adisak Anusornsrirung
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:
然后在用户表单中,您可以从此变量分配或检索值。例如在 userform1 中:
Private Sub btnIDNo_Click()
commonVariable = "UserId"
End Sub
in UserForm2:
在用户窗体 2 中:
Private Sub CommandButton1_Click()
me.txtIDNo.Text = commonVariable
End Sub
回答by DragonSamu
The most simplest way is:
最简单的方法是:
UserForm2.TxtIDNo.Text = UserForm1.txtIDNo.Text
UserForm2.TxtIDNo.Text = UserForm1.txtIDNo.Text

