【问题标题】:Save data from VBA userform into to two sheets将 VBA 用户窗体中的数据保存到两个工作表中
【发布时间】:2018-08-30 08:09:10
【问题描述】:

我正在尝试将在用户表单中输入的数据保存到不同的工作表中。

我目前遇到的问题是,其中一张表 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

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

最好的问候 - 基拉

【问题讨论】:

  • 运行这段代码时会发生什么?在代码的第二部分放一个断点并检查 rw ,它是什么?

标签: excel vba userform


【解决方案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

【讨论】:

  • 完美运行,感谢您的建议,当我要开始我的下一个项目时,我会牢记在心 :) 非常感谢 Xabier :)
  • @Kira 很高兴我能帮上忙... :)
  • 我刚刚发现了一个问题,我无法为组合框设置我的行源,我通过在工作表中创建一个名称(公式 -> 从部分创建)来完成,我通常把在以下代码中: =OFFSET(Hardware!#REF!;0;0;COUNTA(Hardware!$A:$A);1) 但是当我这样做时,用户窗体不会接受名称“PC_Name”作为行源?
  • @KiraJensen 你确定你的公式=OFFSET... 是正确的,因为 #REF 可能不应该存在吗?它可能应该类似于=OFFSET(Hardware!$A$1,0,0,COUNTA(Hardware!$A:$A),COUNTA($1:$1))
  • 我现在无法让它工作?当我按下按钮时,它不会将数据发送到 ws 只发送到 ws2?好像它没有正确读取代码?任何线索为什么会发生这种情况? :)
【解决方案2】:
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

【讨论】:

  • 您声明了 rw1 但从未使用过它,当 excel 中的行数超过 Integer 可以处理的行数时,您将其声明为 Integer ... 也使用 For 循环插入数据效率低下,因为实际匹配条件的行可能是最后一行,因此代码会检查每一行,直到它到达那里......除此之外,这也可以工作。 :)
  • 你是对的@Xabier,我的错。我只是在我自己的 Excel 运行时快速回答 :) rw1 在这里没用
  • ´感谢您的回复西里尔! :)
  • 随时@Kira Jensen 希望对您有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
相关资源
最近更新 更多