繁体   English   中英

我如何通过使用Excel VB在文本框来自两个或多个用户窗体的文本框中输入数据来将数据输入到excel?

[英]How can I input data to excel from inputting data in text box where the text box are from two or more Userform Using Excel VB?

我正在尝试简化财务报告中的数据输入,因此我尝试使用Excel Visual Basic制作表格。

到目前为止,我制作了2个Userform,后来我制作了5个。我制作了userform,以便数据输入操作员可以简化表单的设计,因为文本框很多,然后我将扇区划分为5个用户窗体来简化它。

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

操作员完成所有3个用户窗体的数据输入后,将回到主用户窗体以一次将所有数据输入excel。

我的问题是,我发现很难在用户窗体之间进行连接以从每个用户窗体中获取值,以便最终可以使用主用户窗体或用户窗体1上的1个命令按钮一次将所有值输入至excel。

我的命令按钮代码是这样的:

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

如前所述,如果要使用此方法,则需要将可见性设置为public(在图形编辑器中,其属性为Modifiers

更好的解决方案是在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

最好的方法是以第一种形式创建一个类模块 ,然后以所有其他后续形式访问它。 一个有效的示例可以在下面的屏幕截图中看到。

对不起,无法复制并粘贴确切的代码,因为我只是创建了一个示例,以说明我们如何有效地使用VBA中的类创建数据输入表单。

要添加类模块,请执行以下操作 :菜单->插入->类模块,然后在左下角的“ 属性”窗口中重命名。

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

快乐的VBA :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM