我怎样才能input数据来从input数据文本框中的文本框是从两个或两个以上的用户窗体使用Excel的VB?

我试图缓解从财务报告的数据input,所以我尝试使用Excel的Visual Basic使窗体。

我做了2个Userform到目前为止,后来我做了5.我做了一个用户表单,所以数据录入操作员可以有简单的表单devise,因为文本框太多了,于是我把这个分区分成5个用户表单来简化它。

要在扇区之间移动,操作员可以使用命令button跳转到另一个用户窗体。

当操作员从所有3个用户窗口完成数据input时,他将回到主用户窗口将数据全部input到excel中。

我的问题是,我发现很难连接在userform之间从每个用户窗体获取值,以便最终可以input到excel所有一次使用主要用户窗体或userform1的一个命令button。

我的命令button的代码是这样的:

Private Sub cmdAddData_Click() 'Copy input values to sheet. Dim lRow As Long Dim ws As Worksheet Set ws = Worksheets("Summary") lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row With ws .Cells(lRow, 1).Value = Me.txtNo.Value .Cells(lRow, 2).Value = Me.txtKode.Value .Cells(lRow, 3).Value = Me.txtNamaPerusahaan.Value .Cells(lRow, 4).Value = Me.txtSector.Value .Cells(lRow, 5).Value = Me.txtTime.Value 'UserForm2Begin' .Cells(lRow, 7).Value = Me.txtKas.Value .Cells(lRow, 8).Value = Me.txtInvestasi.Value .Cells(lRow, 9).Value = Me.txtDanaTerbatas.Value .Cells(lRow, 10).Value = Me.txtPiutangUsaha.Value 'UserForm2End' End With 'Clear input controls. Me.txtNo.Value = "" Me.txtKode.Value = "" Me.txtNamaPerusahaan.Value = "" Me.txtSector.Value = "" Me.txtTime.Value = "" 'Userform2Begin' Me.txtKas.Value = "" Me.txtInvestasi.Value = "" Me.txtDanaTerbatas.Value = "" Me.txtPiutangUsaha.Value = "" 'Userform2End' 

提前致谢

如果你声明你想从公共读取的每个文本字段,你可以这样做:

  Class MainForm Form firstPage Form secondPage ... Private Sub cmdAddData_Click() 'Copy input values to sheet. Dim lRow As Long Dim ws As Worksheet Set ws = Worksheets("Summary") lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row With ws .Cells(lRow, 1).Value = firstPage.txtNo.Value .Cells(lRow, 2).Value = firstPage.txtKode.Value .Cells(lRow, 3).Value = firstPage.txtNamaPerusahaan.Value .Cells(lRow, 4).Value = firstPage.txtSector.Value .Cells(lRow, 5).Value = firstPage.txtTime.Value 'UserForm2Begin' .Cells(lRow, 7).Value = secondPage.txtKas.Value .Cells(lRow, 8).Value = secondPage.txtInvestasi.Value .Cells(lRow, 9).Value = secondPage.txtDanaTerbatas.Value .Cells(lRow, 10).Value = secondPage.txtPiutangUsaha.Value 'UserForm2End' End With 'Clear input controls. Me.txtNo.Value = "" Me.txtKode.Value = "" Me.txtNamaPerusahaan.Value = "" firstPage.txtSector.Value = "" firstPage.txtTime.Value = "" 'Userform2Begin' secondPage.txtKas.Value = "" secondPage.txtInvestasi.Value = "" secondPage.txtDanaTerbatas.Value = "" secondPage.txtPiutangUsaha.Value = "" End Sub End Class 

如上所述,如果你想使用这种方法,你需要设置公开的可见性(在graphics编辑器中它的属性Modifiers

更好的解决scheme是在Mainfrom中声明一个对象,该对象具有以后要在Excel文件中拥有的所有值的属性。 然后把这个对象赋给每一个表单,例如在构造函数中,然后填充它。 在您的Mainform中,您可以读取对象的所有属性并将其写入文件。 保存你的数据的对象将是这样的:

  Class DataObject Public txtNo as String Public txtKode as String ... End Class 

您可以在用户可以看到的第一个表单中声明该表单,然后将其提供给随后的所有表单

 Class FirstForm Dim data as DataObject ... private sub openNextWindow() dim sec as SecondForm= new SecondForm(DataObject) ... end sub end class 

直到你终于在你的cmdAddData,你这样做:

 Private Sub cmdAddData_Click() 'Copy input values to sheet. Dim lRow As Long Dim ws As Worksheet Set ws = Worksheets("Summary") lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row With ws .Cells(lRow, 1).Value = data.txtNo .Cells(lRow, 2).Value = data.txtKode ... End Sub 

最好的办法是在第一个表格中创build一个类模块 ,并以后续的其他forms访问它。 在下面的屏幕截图中可以看到一个工作示例。

抱歉,无法复制和粘贴确切的代码,因为它只是我创build的一个示例,以显示如何有效地使用VBA中的类创build数据input表单。

要添加类模块 :菜单 – >插入 – >类模块,并在位于左下angular的属性窗口中重命名它。

在Excel中从用户表单输入数据

快乐的VBA 🙂