在用户窗体之间传递数据 [英] Pass data between UserForms

查看:56
本文介绍了在用户窗体之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Excel VBA中,我有一个类似于以下内容的用户表单,其中用户输入ID号,然后在用户表单上显示详细信息:

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 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

在详细信息用户表单"上,我有一个编辑"按钮.单击编辑"按钮将打开另一个用户表单,用户可以在其中更改该ID号的详细信息,但显然不能更改ID号本身.为此,我需要将ID号从详细信息用户表单"传递到编辑用户表单".有办法吗?

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?

显示详细信息"用户表单底部的打开编辑用户表单"的内容类似于以下内容:

The bottom on the Show Details User Form to open the Edit User Form is similar to the following:

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://www.mrexcel.com/forum/excel-questions/671964-visual-basic-applications-pass-variables-between-user-forms.html

http://gregmaxey.mvps.org/word_tip_pages/userform_pass_data.html

http://peltiertech.com/Excel/PropertyProcedures.html

推荐答案

有很多方法...这里有一些...

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

方法1

  1. 在模块中声明一个Public变量
  2. 在Userform1中分配该变量,然后启动Userform2.该变量将保留其值.例子

在Userform1中

In Userform1

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

在Userform2中

In Userform2

Private Sub CommandButton1_Click()
    MsgBox MyVal
End Sub

在模块中

Public MyVal

方法2

使用用户窗体的.Tag属性

在Userform1中

In Userform1

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

在Userform2中

In Userform2

Private Sub CommandButton1_Click()
    MsgBox Me.Tag
End Sub

方法3

在Userform2中添加Label并将其可见属性设置为False

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

在Userform1中

In Userform1

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

在Userform2中

In Userform2

Private Sub CommandButton1_Click()
    MsgBox Label1.Caption
End Sub

这篇关于在用户窗体之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆