在VBA上的Userforms之间传递多个条目

时间:2017-02-17 16:54:19

标签: excel forms vba excel-vba

所以我正在尝试创建用户表单。我目前有两种形式,第一种是卡车的一般数据,一旦用户输入了所有相应的文本框,第二个用户形式将显示并将数据传输到卡车信息标签上以供参考。 (我把这个转移弄明白了)

1st User Form

我希望用户能够添加多个部件号和&每个特定/唯一预告片的数量(通常有超过1个部分)。因此,当用户单击“添加部件号”按钮时,它将在细节中添加一个新行,并将焦点返回到部件号组合框,就像我对第一个用户表单所做的那样,这样他们就可以从下一部分开始了

2nd Form

这是第一张表格的代码

Private Sub UserForm1_Initialize()
txtTrailerNum.SetFocus
txtTrailerNum.Value = ""
txtScacCode.Value = ""
txtTruckNum.Value = ""
txtSupNum.Value = ""
txtInvoiceNum.Value = ""
txtInvoiceType.Value = ""
txtOrgRef.Value = ""
 End Sub

Private Sub Clear_btn_Click()
txtTrailerNum.Value = ""
txtScacCode.Value = ""
txtTruckNum.Value = ""
txtSupNum.Value = ""
txtInvoiceNum.Value = ""
txtInvoiceType.Value = ""
txtOrgRef.Value = ""
 End Sub

Private Sub Enter_btn_Click()

If txtTrailerNum.Text = "" Then
    MsgBox "Please Enter a valid Trailer Number."
    txtTrailerNum.SetFocus
    Exit Sub
End If

If txtScacCode.Text = "" Then
    MsgBox "Please Enter a valid SCAC Code."
    txtScacCode.SetFocus
    Exit Sub
End If

If txtTruckNum.Text = "" Then
    MsgBox "Please Enter a valid Truck Number."
    txtTruckNum.SetFocus
    Exit Sub
End If

If txtSupNum.Text = "" Then
    MsgBox "Please Enter a Supplier Number."
    txtSupNum.SetFocus
    Exit Sub
End If

If txtInvoiceNum.Text = "" Then
    MsgBox "Please Enter a valid Invoice Number."
    txtInvoiceNum.SetFocus
    Exit Sub
End If

If txtInvoiceType.Text = "" Then
    MsgBox "Please Enter a valid Invoice Number."
    txtInvoiceType.SetFocus
    Exit Sub
End If

If txtOrgRef.Text = "" Then
    MsgBox "Please Enter a valid Oiginator Reference."
    txtOrgRef.SetFocus
    Exit Sub
End If

UserForm2.Show

End Sub

以下是我的第二张表格的代码

Private Sub AddPrtNumbtn_Click()

End Sub

Private Sub PartNumcbo_AfterUpdate()
On Error Resume Next         
txtPartDesc=Application.WorksheetFunction.VLookup(PartNumcbo.Value,
                                     Range("PartDesc"), 2, False)
If Err.Number <> 0 Then
MsgBox "Invalid Part Number"
txtPartDesc.Value = ""
 End If
 On Error GoTo 0
 End Sub


 Private Sub UserForm_Initialize()
 PartNumcbo.RowSource = "PartDescData!A:A"
 TruckInfolbl.Caption = "Trailer Num: " & UserForm1.txtTrailerNum.Value & "    
 " & "SCAC Code: " & UserForm1.txtScacCode.Value & " " & "Truck Number: " &   
 UserForm1.txtTruckNum.Value & "  " & "Supplier Number: " & 
 UserForm1.txtSupNum.Value & " " & "Invoice Number: " & 
 UserForm1.txtInvoiceNum & " " & "Invoice Type: " & 
 UserForm1.txtInvoiceType.Value & " " & "Originator Reference: " & 
 UserForm1.txtOrgRef.Value

 End Sub

理想情况下,当表格完成后,零件编号将与第一张表格中的卡车相关,然后输入到工作表范围内。因此,如果用户单击第二个表单底部的创建预告片按钮,则会将其输入到工作表中。

如何将组合框中的数据和第二个Userform上的两个txtbox传递到Inventory Details Label,然后将其输入到一个范围内???

1 个答案:

答案 0 :(得分:2)

一个表单将是 parent 和一个 child 。家长将参考孩子并将创建它。当孩子完成其工作时,它将使用已收集的所有数据引发事件。父母将订阅此事件,因此它将接收数据。

注意: 正如@Comintern所说,将所有数据包装在类中并传递此类的实例可能是合适的。

这是一个例子,HTH。

  

父表格

Option Explicit

Private WithEvents m_childForm As UserFormChild

Private Sub CommandButton1_Click()
    Set m_childForm = New UserFormChild
    m_childForm.Show
End Sub

Private Sub m_childForm_ProcessData(ByVal comboBoxData As String, ByVal textBoxData1 As String, ByVal textBoxData2 As String)
   Me.TextBox1.Text = "Data: " & comboBoxData & ", " & textBoxData1 & ", " & textBoxData2
End Sub
  

名为UserFormChild的子表单

Option Explicit

Public Event ProcessData(ByVal comboBoxData As String, ByVal textBoxData1 As String, ByVal textBoxData2 As String)

Private Sub CommandButton1_Click()
    RaiseEvent ProcessData(Me.ComboBox1.Text, Me.TextBox1.Text, Me.TextBox2.Text)
    Unload Me
End Sub

Private Sub CommandButton2_Click()
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Me.ComboBox1.AddItem "A"
    Me.ComboBox1.AddItem "B"
    Me.ComboBox1.AddItem "C"
    Me.ComboBox1.ListIndex = 1
End Sub

enter image description here

enter image description here