Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用excel VB在文本框中输入来自两个或多个用户表单的数据,从而将数据输入excel?_Excel_Vba - Fatal编程技术网

如何使用excel VB在文本框中输入来自两个或多个用户表单的数据,从而将数据输入excel?

如何使用excel VB在文本框中输入来自两个或多个用户表单的数据,从而将数据输入excel?,excel,vba,Excel,Vba,我试图简化财务报告中的数据输入,因此我尝试使用Excel Visual Basic制作表单 到目前为止,我制作了2个用户表单,稍后我将制作5个。我创建了userform,这样数据输入操作符就可以对表单进行简单的设计,因为文本框太多了,所以我将该扇区划分为5个userform以简化它 要在扇区之间移动,操作员可以使用命令按钮跳转到另一个用户窗体 当操作员完成所有3个用户表单的数据输入后,他将返回主用户表单,将所有数据一次性输入excel 我的问题是,我发现很难在userform之间进行连接,以便从

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

到目前为止,我制作了2个用户表单,稍后我将制作5个。我创建了userform,这样数据输入操作符就可以对表单进行简单的设计,因为文本框太多了,所以我将该扇区划分为5个userform以简化它

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

当操作员完成所有3个用户表单的数据输入后,他将返回主用户表单,将所有数据一次性输入excel

我的问题是,我发现很难在userform之间进行连接,以便从每个userform获取值,从而最终可以使用主userform或userform1上的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
    Class DataObject
        Public txtNo as String
        Public txtKode as String
        ...
    End Class
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
如前所述,如果要使用此方法,则需要将可见性设置为public(在图形编辑器中,其属性为
修改器

更好的解决方案是在Mainfrom中声明一个对象,该对象具有以后要在Excel文件中包含的所有值的属性。然后将这个对象赋给每个表单,例如构造函数中的表单,并填充它。 在Mainform中,您可以读取对象的所有属性并将它们写入文件。 用于保存数据的对象如下所示:

 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
    Class DataObject
        Public txtNo as String
        Public txtKode as String
        ...
    End Class
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
您在用户可以看到的第一个表单中声明它,然后将它提供给后面的每个表单

Class FirstForm
   Dim data as DataObject
   ...

   private sub openNextWindow()
       dim sec as SecondForm= new SecondForm(DataObject)
       ...
   end sub
end class
直到您最终到达cmdAddData,您可以这样做:

 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
    Class DataObject
        Public txtNo as String
        Public txtKode as String
        ...
    End Class
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中的类创建数据输入表单

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


快乐VBA:-)

为什么不使用一个对象来保存您在多个表单中输入的所有数据,然后使用此对象将所有内容打印到excel?先生,我如何使用该对象?我希望它比上面的代码更简单,因为我是excel VB的新手,上面的代码,我只是从互联网上复制并转换它以供我的数据使用。谢谢你的回答,不会这么简单,因为你需要使用Actionlisteners作为按钮。但这将是一种更干净的方式。一个简单但肮脏的解决方案,我会把你作为答案发布。好的,先生,我该怎么做?你为什么不使用多页控件而不是创建更多的用户表单呢。与通过VBA代码将信息存储在内存中相比,使此控件的页面之间的信息可用可能要容易得多。在我的Excel VB中声明为文本的类,我如何处理该类,先生?请参阅更新的答案,如果您适合使用反射,您甚至可以在“带ws”部分简化代码,但这是一个更复杂的主题,所以我最好不要在这里使用它。啊,是的,这也有明显的帮助。我只是不太了解这门课,我想弄明白。谢谢!如果这种方法有助于你实现目标,请将答案标记为已接受,以便将来帮助他人。我已标记了以前的答案。我怎样才能将其标记为已接受,因为这个答案也有助于我的视觉效果