如何将新信息放在下一个空行?

huangapple go评论101阅读模式
英文:

My VBA code overwrites my last row. How can I write the code to put new information on the next empty row?

问题

我跟随一个YouTube教程来将一个工作簿中一个工作表的数据复制到另一个工作表中。请注意,我对VBA没有经验。

我注意到当我添加新条目时,它会覆盖之前的行。我希望代码能够找到最后一个空行并将最新的条目添加到那里。所以,如果第1行和第2行有信息,那么最新的信息将被传送到第3行,依此类推。

以下是代码:

  1. Private Sub CommandButton1_Click()
  2. '创建并设置时间追踪表和主数据表的变量
  3. Dim TTrk As Worksheet, MDat As Worksheet
  4. Set TTrk = Sheet1
  5. Set MDat = Sheet2
  6. '为时间追踪表中的每个单元格创建并设置变量
  7. Dim Name As Range, AddDate As Range, TotalHours As Range, Activity As Range, Client As Range, Category As Range, AddHours As Range, TimeSpent As Range, Additional As Range
  8. Set Name = TTrk.Range("C2")
  9. Set AddDate = TTrk.Range("C3")
  10. Set TotalHours = TTrk.Range("C5")
  11. Set Activity = TTrk.Range("B8:B15")
  12. Set Client = TTrk.Range("C8:C15")
  13. Set Category = TTrk.Range("D8:D15")
  14. Set AddHours = TTrk.Range("E8:E15")
  15. Set TimeSpent = TTrk.Range("F8:F15")
  16. Set Additional = TTrk.Range("G8:G15")
  17. '创建主数据表中粘贴单元格的变量
  18. Dim DestCell As Range
  19. If MDat.Range("A2") = "" Then '如果A2为空
  20. Set DestCell = MDat.Range("A2") '...那么目标单元格是A2
  21. Else
  22. Set DestCell = MDat.Range("A1").End(xlDown).Offset(1, 0) '...否则,下一个空行
  23. If MDat.Range("A2") = "" Then '如果A2为空
  24. Set DestCell = MDat.Range("A2") '...那么目标单元格是A2
  25. Else
  26. Set DestCell = MDat.Range("A1").End(xlDown).Offset(2, 0) '...否则,下一个空行
  27. End If
  28. End If
  29. '从时间追踪表复制并粘贴数据到主数据表
  30. Name.Copy DestCell
  31. AddDate.Copy DestCell.Offset(0, 1)
  32. TotalHours.Copy DestCell.Offset(0, 2)
  33. Activity.Copy DestCell.Offset(0, 3)
  34. Client.Copy DestCell.Offset(0, 4)
  35. Category.Copy DestCell.Offset(0, 5)
  36. AddHours.Copy DestCell.Offset(0, 6)
  37. TimeSpent.Copy DestCell.Offset(0, 7)
  38. Additional.Copy DestCell.Offset(0, 8)
  39. '清除时间追踪表中的内容
  40. AddDate.ClearContents
  41. TotalHours.ClearContents
  42. Activity.ClearContents
  43. Client.ClearContents
  44. Category.ClearContents
  45. AddHours.ClearContents
  46. TimeSpent.ClearContents
  47. Additional.ClearContents
  48. End Sub

我尝试去掉某些代码并尝试找到可以添加到我的当前代码中的代码,以查看是否可以解决问题,但我对VBA没有经验。

英文:

I followed a YouTube tutorial to copy data from one sheet into another sheet in the same workbook. Mind you, I have no experience in VBA.

I noticed that when I add a new entry it overwrites the previous row. I want the code to find the last empty row and add the newest entry there. So, if rows 1 and 2 have information, then the newest information gets transferred to row 3 and so forth.

Here is the code:

  1. Private Sub CommandButton1_Click()
  2. 'Create and set variables for the Time Tracker & Master Data worksheets
  3. Dim TTrk As Worksheet, MDat As Worksheet
  4. Set TTrk = Sheet1
  5. Set MDat = Sheet2
  6. 'Create and set variables for each cell in the Time Tracker sheet
  7. Dim Name As Range, AddDate As Range, TotalHours As Range, Activity As Range, Client As Range, Category As Range, AddHours As Range, TimeSpent As Range, Additional As Range
  8. Set Name = TTrk.Range("C2")
  9. Set AddDate = TTrk.Range("C3")
  10. Set TotalHours = TTrk.Range("C5")
  11. Set Activity = TTrk.Range("B8:B15")
  12. Set Client = TTrk.Range("C8:C15")
  13. Set Category = TTrk.Range("D8:D15")
  14. Set AddHours = TTrk.Range("E8:E15")
  15. Set TimeSpent = TTrk.Range("F8:F15")
  16. Set Additional = TTrk.Range("G8:G15")
  17. 'Create a variable for the paste cell in the Master Data worksheet
  18. Dim DestCell As Range
  19. If MDat.Range("A2") = "" Then 'If A2 is empty
  20. Set DestCell = MDat.Range("A2") '...then destination cell is A2
  21. Else
  22. Set DestCell = MDat.Range("A1").End(xlDown).Offset(1, 0) '...otherwise the next empty row
  23. If MDat.Range("A2") = "" Then 'If A2 is empty
  24. Set DestCell = MDat.Range("A2") '...then destination cell is A2
  25. Else
  26. Set DestCell = MDat.Range("A1").End(xlDown).Offset(2, 0) '...otherwise the next empty row
  27. End If
  28. 'Copy and paste data from the Time Tracker worksheet to the Master Data worksheet
  29. Name.Copy DestCell
  30. AddDate.Copy DestCell.Offset(0, 1)
  31. TotalHours.Copy DestCell.Offset(0, 2)
  32. Activity.Copy DestCell.Offset(0, 3)
  33. Client.Copy DestCell.Offset(0, 4)
  34. Category.Copy DestCell.Offset(0, 5)
  35. AddHours.Copy DestCell.Offset(0, 6)
  36. TimeSpent.Copy DestCell.Offset(0, 7)
  37. Additional.Copy DestCell.Offset(0, 8)
  38. 'Clear the contents in the Time Tracker worksheet
  39. AddDate.ClearContents
  40. TotalHours.ClearContents
  41. Activity.ClearContents
  42. Client.ClearContents
  43. Category.ClearContents
  44. AddHours.ClearContents
  45. TimeSpent.ClearContents
  46. Additional.ClearContents
  47. End Sub

I tried taking away certain coding and trying to find coding to add into my current code to see if it would resolve the issue, but I have no experience in VBA.

答案1

得分: 2

  1. 将范围侧-by-侧复制
  2. -
  3. **一个快速修复**
  4. - 以以下方式引用目标单元格
  5. <!-- 语言lang-vb -->
  6. Dim DestCell As Range: Set DestCell = MDat.Range("A2")
  7. With MDat.UsedRange
  8. Dim drOffset As Long:
  9. drOffset = .Rows(.Rows.Count).Row - DestCell.Row + 1
  10. If drOffset > 0 Then
  11. Set DestCell = DestCell.Offset(drOffset)
  12. End If
  13. End With
  14. **之前**
  15. [![在这里输入图片描述][1]][1]
  16. **之后**
  17. [![在这里输入图片描述][2]][2]
  18. **一个简化**
  19. <!-- 语言lang-vb -->
  20. Private Sub CommandButton1_Click()
  21. Const SRC_RANGES As String = "C2,C3,C5,B8:G15"
  22. Dim sClearFlags(): sClearFlags = VBA.Array(0, 1, 1, 1)
  23. Const DST_FIRST_CELL As String = "A2"
  24. Dim sws As Worksheet: Set sws = Sheet1
  25. Dim dws As Worksheet: Set dws = Sheet2
  26. Dim dfCell As Range: Set dfCell = dws.Range(DST_FIRST_CELL)
  27. With dws.UsedRange
  28. Dim drOffset As Long: drOffset = .Rows(.Rows.Count).Row - dfCell.Row + 1
  29. If drOffset > 0 Then
  30. Set dfCell = dfCell.Offset(drOffset)
  31. End If
  32. End With
  33. With sws.Range(SRC_RANGES)
  34. Dim arg As Range, n As Long
  35. For Each arg In .Areas
  36. ' 复制.
  37. arg.Copy dfCell
  38. Set dfCell = dfCell.Offset(, arg.Columns.Count)
  39. ' 清除.
  40. If sClearFlags(n) = 1 Then
  41. arg.ClearContents
  42. End If
  43. n = n + 1
  44. Next arg
  45. End With
  46. End Sub
  47. [1]: https://i.stack.imgur.com/qI1HJ.jpg
  48. [2]: https://i.stack.imgur.com/PLscV.jpg
英文:

Copy Ranges Side-By-Side

A Quick Fix

  • Reference the destination cell in the following way:

<!-- language: lang-vb -->

  1. Dim DestCell As Range: Set DestCell = MDat.Range(&quot;A2&quot;)
  2. With MDat.UsedRange
  3. Dim drOffset As Long:
  4. drOffset = .Rows(.Rows.Count).Row - DestCell.Row + 1
  5. If drOffset &gt; 0 Then
  6. Set DestCell = DestCell.Offset(drOffset)
  7. End If
  8. End With

Before

如何将新信息放在下一个空行?

After

如何将新信息放在下一个空行?

A Simplification

<!-- language: lang-vb -->

  1. Private Sub CommandButton1_Click()
  2. Const SRC_RANGES As String = &quot;C2,C3,C5,B8:G15&quot;
  3. Dim sClearFlags(): sClearFlags = VBA.Array(0, 1, 1, 1)
  4. Const DST_FIRST_CELL As String = &quot;A2&quot;
  5. Dim sws As Worksheet: Set sws = Sheet1
  6. Dim dws As Worksheet: Set dws = Sheet2
  7. Dim dfCell As Range: Set dfCell = dws.Range(DST_FIRST_CELL)
  8. With dws.UsedRange
  9. Dim drOffset As Long: drOffset = .Rows(.Rows.Count).Row - dfCell.Row + 1
  10. If drOffset &gt; 0 Then
  11. Set dfCell = dfCell.Offset(drOffset)
  12. End If
  13. End With
  14. With sws.Range(SRC_RANGES)
  15. Dim arg As Range, n As Long
  16. For Each arg In .Areas
  17. &#39; Copy.
  18. arg.Copy dfCell
  19. Set dfCell = dfCell.Offset(, arg.Columns.Count)
  20. &#39; Clear.
  21. If sClearFlags(n) = 1 Then
  22. arg.ClearContents
  23. End If
  24. n = n + 1
  25. Next arg
  26. End With
  27. End Sub

huangapple
  • 本文由 发表于 2023年7月6日 13:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定