将VBA用户表单中的数据保存到两张表中

时间:2018-08-30 08:09:10

标签: excel excel-vba userform

我正在尝试将在用户表单中输入的数据保存到其他网表中。

我当前遇到的问题是,VBA的一张纸必须查找要添加的特定行,但是另一张纸将是插入数据的历史记录,因此需要将数据插入下一个空闲行。

我有这段代码可用于查找并插入到第一张纸中:

Private Sub pSave()

Dim rw As Integer
Dim ws As Worksheet
    Set ws = Worksheets("Hardware")

    'Takting the inserted values from the userform and inserting them into the spreadsheet

        totRows = Worksheets("Hardware").Range("A4").CurrentRegion.Rows.Count

        For i = 2 To totRows
            If Trim(Worksheets("Hardware").Cells(i, 1)) = Trim(ComboBox_PCNameChoose.Value) Then
            'Inserting them into the Hardware sheet (The main sheet)
                Worksheets("Hardware").Cells(i, 12).Value = TextBox_Name.Text
                Worksheets("Hardware").Cells(i, 13).Value = TextBox_Email.Text
                Worksheets("Hardware").Cells(i, 14).Value = TextBox_PhoneNumber.Text
                Worksheets("Hardware").Cells(i, 15).Value = DTPicker_Borrow.Value
                Worksheets("Hardware").Cells(i, 16).Value = DTPicker_Return.Value

            Exit For
        End If
    Next i

我知道这在另一种将数据插入下一个空闲行的用户窗体中有效,但是我无法弄清楚如何在同时保存两张纸时使其工作

Dim rw As Integer
Dim ws2 As Worksheet

Set ws2 = Worksheets("Rental_History")
If rw = ws2.Cells.Find(What:="*", Searchorder:=xlRows, SearchDirection:=Previous, LookIn:=xlValues).Row + 1 Then
    ws2.Cells(rw, 10).Value = TextBox_Name.Text
    ws2.Cells(rw, 11).Value = TextBox_Email.Text
    ws2.Cells(rw, 12).Value = TextBox_PhoneNumber.Text
    ws2.Cells(rw, 13).Value = DTPicker_Borrow.Value
    ws2.Cells(rw, 14).Value = DTPicker_Return.Value
End If

提前,谢谢您的时间和帮助! :)

最佳问候 -基拉

2 个答案:

答案 0 :(得分:1)

我相信以下内容将达到您的期望,而不是使用For循环查找要在其中添加第一行数据的行。我使用了.Find方法,因为这样做会更快,而不是循环遍历直到找到匹配的每一行,find方法都会快速跳到匹配的行。

还要特别注意,我将rw的声明从Integer更改为Long,因为Excel中的单元格数量超过了Integer变量所能处理的范围:

Private Sub pSave()
Dim rw As Long
Dim ws As Worksheet: Set ws = Worksheets("Hardware")
Dim ws2 As Worksheet: Set ws2 = Worksheets("Rental_History")
Dim foundval As Range

'Taking the inserted values from the userform and inserting them into the spreadsheet
Set foundval = ws.Range("A:A").Find(What:=Trim(ComboBox_PCNameChoose.Value)) 'find the value that matches
If Not foundval Is Nothing Then 'if found, use that row to insert data
    'Inserting them into the Hardware sheet (The main sheet)
    ws.Cells(foundval.Row, 12).Value = TextBox_Name.Text
    ws.Cells(foundval.Row, 13).Value = TextBox_Email.Text
    ws.Cells(foundval.Row, 14).Value = TextBox_PhoneNumber.Text
    ws.Cells(foundval.Row, 15).Value = DTPicker_Borrow.Value
    ws.Cells(foundval.Row, 16).Value = DTPicker_Return.Value
End If

rw = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1
'get the next free row
ws2.Cells(rw, 10).Value = TextBox_Name.Text
ws2.Cells(rw, 11).Value = TextBox_Email.Text
ws2.Cells(rw, 12).Value = TextBox_PhoneNumber.Text
ws2.Cells(rw, 13).Value = DTPicker_Borrow.Value
ws2.Cells(rw, 14).Value = DTPicker_Return.Value
End Sub

答案 1 :(得分:0)

Dim rw As Integer
Dim ws As Worksheet
Set ws = Worksheets("Hardware")
Dim rw1 As Integer
Dim ws2 As Worksheet
Set ws2 = Worksheets("Rental_History")

'Takting the inserted values from the userform and inserting them into the spreadsheet

    totRows = Worksheets("Hardware").Range("A4").CurrentRegion.Rows.Count

    For i = 2 To totRows
        If Trim(Worksheets("Hardware").Cells(i, 1)) = Trim(ComboBox_PCNameChoose.Value) Then
        'Inserting them into the Hardware sheet (The main sheet)
            rw = ws2.Cells.Find(What:="*", Searchorder:=xlRows, SearchDirection:=Previous, LookIn:=xlValues).Row + 1 'updates rw  as it changes at each loop
            ws.Cells(i, 12).Value = TextBox_Name.Text
            ws2.Cells(rw, 10).Value = ws.Cells(i, 12).Value
            ws.Cells(i, 13).Value = TextBox_Email.Text
            ws2.Cells(rw, 11).Value = ws.Cells(i, 13).Value
            ws.Cells(i, 14).Value = TextBox_PhoneNumber.Text
            ws2.Cells(rw, 12).Value = ws.Cells(i, 14).Value
            ws.Cells(i, 15).Value = DTPicker_Borrow.Value
            ws2.Cells(rw, 13).Value = ws.Cells(i, 15).Value
            ws.Cells(i, 16).Value = DTPicker_Return.Value
            ws2.Cells(rw, 14).Value = ws.Cells(i, 16).Value
       End If
   Next i