簡體   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