【问题标题】:Pass data between UserForms在用户窗体之间传递数据
【发布时间】:2015-12-07 20:14:14
【问题描述】:

在 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

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

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

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

我已经查看了以下链接,但它们似乎没有帮助:

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

【问题讨论】:

  • 您是否考虑过将当前的 2 个表单实现为具有多页控件的单个表单?您可以将当前位于第一个表单上的字段放在多页的第 1 页上,将当前位于第二个表单上的字段放在第 2 页上。您可以在默认情况下使第 2 页不可见,并根据需要在单个表单中的代码中进行控制显示第 2 页

标签: forms vba excel userform


【解决方案1】:

有几种方法可以解决这个问题。 我使用的是在模块中声明全局或公共变量

例子:

Public commonVariable As String

然后在用户表单中,您可以从该变量中分配或检索值。 例如 在用户窗体 1 中:

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

在 UserForm2 中:

Private Sub CommandButton1_Click()
    me.txtIDNo.Text = commonVariable 
End Sub

【讨论】:

    【解决方案2】:

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

    方式 1

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

    在用户窗体1中

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

    在 Userform2 中

    Private Sub CommandButton1_Click()
        MsgBox MyVal
    End Sub
    

    在模块中

    Public MyVal
    

    方式 2

    使用用户窗体的.Tag 属性

    在用户窗体1中

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

    在 Userform2 中

    Private Sub CommandButton1_Click()
        MsgBox Me.Tag
    End Sub
    

    方式 3

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

    在用户窗体1中

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

    在 Userform2 中

    Private Sub CommandButton1_Click()
        MsgBox Label1.Caption
    End Sub
    

    【讨论】:

    • 最简单的方法不是UserForm2.Txtbox.Text = UserForm1.txtIDNo.Text
    • 是的 :) 我回答了“如何在用户表单之间传递数据?”的问题。就您的回答而言,我已经在Way 3 中演示了类似的概念:)
    【解决方案3】:

    最简单的方法是:

    UserForm2.TxtIDNo.Text = UserForm1.txtIDNo.Text

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多