如何复制并粘贴最后一行直到特定日期?

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

How to copy and paste the last row until a certain date?

问题

I am fairly newer to VBA. 我对VBA相对较新。
I took a class in college and that has been years now since using it. 我在大学上了一门课,现在已经多年没有使用它了。
I am trying to copy the last row in a spreadsheet within columns A through S multiple times. 我尝试多次复制电子表格中从A列到S列的最后一行。
I am needing to do this from the date specified in the last cell in column A until the date I specify in cell A1. 我需要从列A中最后一个单元格中指定的日期开始,一直复制到我在A1单元格中指定的日期。
This is going to be a report I update weekly is why I am trying to do this. 这将是一个我每周更新的报告,这就是为什么我要这样做的原因。
I am needing to copy all of the formulas in the last row. 我需要复制最后一行中的所有公式。
I got this to work as a simple copy and paste. 我已经将这个工作做成了简单的复制和粘贴。
But once I did the if statement, it just does nothing with no error. 但是一旦我使用了if语句,它就什么都不做,没有错误。Any idea what I'm doing wrong? 有什么想法我做错了吗?

英文:

I am fairly newer to VBA. I took a class in college and that has been years now since using it. I am trying to copy the last row in a spreadsheet within columns A through S multiple times. I am needing to do this from the date specified in the last cell in column A until the date I specify in cell A1. This is going to be a report I update weekly is why I am trying to do this. I am needing to copy all of the formulas in the last row.

  1. Public Sub CopyLastRow()
  2. ' Define the source sheet (no need to activate it)
  3. Dim sourceSheet As Worksheet
  4. Set sourceSheet = ThisWorkbook.Worksheets("Steam Data")
  5. ' Find the last row in an specific column
  6. Dim lastRow As Long
  7. lastRow = sourceSheet.Range("A" & sourceSheet.Rows.Count).End(xlUp).Row
  8. ' Set the source range according to columns and last row
  9. Dim sourceRange As Range
  10. Set sourceRange = sourceSheet.Range("A" & lastRow & ":S" & lastRow)
  11. Dim x As Integer
  12. Dim lcell As Long
  13. lcell = Cells(Rows.Count, 1).End(xlUp).Row
  14. For x = lcell To x = Range("A1")
  15. If x < Range("A1") Then
  16. sourceRange.Copy
  17. sourceRange.Offset(1).PasteSpecial
  18. Else
  19. End If
  20. Next x
  21. End Sub

I got this to work as a simple copy and paste. But once I did the if statement, it just does nothing with no error. Any idea what I'm doing wrong?

答案1

得分: 1

如果我理解你最后的评论,下面的代码将完成你需要的任务。它可以放入一个模块或者"Steam Data"工作表的代码部分。不需要激活"Steam Data"工作表来运行这段代码:

  1. Public Sub CopyLastRow()
  2. Dim ws As Worksheet, _
  3. rngSourceRange As Range, _
  4. rngTargetRange As Range, _
  5. lngLastRow As Long, _
  6. lngRowsToAdd As Long, _
  7. dteTargetDate As Date, _
  8. dteLastDate As Date
  9. ' 定义我们要操作的工作表
  10. Set ws = ThisWorkbook.Worksheets("Steam Data")
  11. ' 确定目标日期
  12. ' 这将是你的“指定日期”,可以是单元格引用、传递给这个过程的参数,或者任何其他可能的来源。这里只是一个示例中硬编码的日期。
  13. dteTargetDate = #6/24/2023#
  14. With ws
  15. ' 确定列A中的最后一行
  16. lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
  17. ' 确保我们找到了有效日期。如果没有,显示一条消息并退出。
  18. If IsDate(.Cells(lngLastRow, 1)) = False Then
  19. MsgBox "最后一行不包含有效日期。进程中止。"
  20. Exit Sub
  21. End If
  22. ' 计算我们需要添加的行数(天数)
  23. lngRowsToAdd = dteTargetDate - .Cells(lngLastRow, 1)
  24. ' 定义源和目标范围
  25. Set rngSourceRange = .Range("A" & lngLastRow)
  26. Set rngTargetRange = .Range("A" & lngLastRow & ":A" & lngLastRow + lngRowsToAdd)
  27. ' 使用“填充序列”选项将所选日期复制到目标范围
  28. rngSourceRange.AutoFill Destination:=rngTargetRange
  29. ' 选择并复制我们要重复的最后一行的列
  30. .Range("B" & lngLastRow & ":S" & lngLastRow).Copy
  31. ' 粘贴行
  32. .Paste Destination:=.Range("B" & lngLastRow + 1 & ":B" & lngLastRow + lngRowsToAdd)
  33. End With
  34. Set ws = Nothing
  35. End Sub

有关更多信息,我不确定你是否熟悉开发者选项卡下的“录制宏”选项。这是一个非常有用的工具,可以基于你的按键生成基本的代码。生成的代码可能会有一些不必要的步骤,但可以进行清理。

英文:

If I understood your last comment, the code below will do what you need. It can go into a module or the code section of the "Steam Data" worksheet. The "Steam Data" worksheet does not need to be active to run the code:

  1. Public Sub CopyLastRow()
  2. Dim ws As Worksheet, _
  3. rngSourceRange As Range, _
  4. rngTargetRange As Range, _
  5. lngLastRow As Long, _
  6. lngRowsToAdd As Long, _
  7. dteTargetDate As Date, _
  8. dteLastDate As Date
  9. ' Define the sheet we want to work with
  10. Set ws = ThisWorkbook.Worksheets("Steam Data")
  11. ' Establish the target date
  12. ' This will be your 'specified date' and can be a cell reference, a parameter
  13. ' passed into this procedure, or any other possible source. I have just hardcoded
  14. ' a date here as an example.
  15. dteTargetDate = #6/24/2023#
  16. With ws
  17. ' Determine the last row in Column A
  18. lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
  19. ' Make sure we have found a valid date. If not then show a message and get out.
  20. If IsDate(.Cells(lngLastRow, 1)) = False Then
  21. MsgBox "The last row does not contain a valid date. Process aborted."
  22. Exit Sub
  23. End If
  24. ' Calculate the number of rows (days) we need to add
  25. lngRowsToAdd = dteTargetDate - .Cells(lngLastRow, 1)
  26. ' Define the source and target ranges
  27. Set rngSourceRange = .Range("A" & lngLastRow)
  28. Set rngTargetRange = .Range("A" & lngLastRow & ":A" & lngLastRow + lngRowsToAdd)
  29. ' Copy the selected date to the target range with the 'Fill Series' option
  30. rngSourceRange.AutoFill Destination:=rngTargetRange
  31. ' Select and copy the columns from the last row that we want to repeat
  32. .Range("B" & lngLastRow & ":S" & lngLastRow).Copy
  33. ' Paste the rows
  34. .Paste Destination:=.Range("B" & lngLastRow + 1 & ":B" & lngLastRow + lngRowsToAdd)
  35. End With
  36. Set ws = Nothing
  37. End Sub

For further info, I am not sure if you are familiar with the Record Macro option under the Developer Tab. This is a very useful tool for generating bare-bones code based on your keystrokes. The resulting code can be somewhat verbose with steps that may not be necessary but this can be cleaned up.

答案2

得分: 1

复制(追加)最后一行

快速修复(保留思路)

  1. Sub CopyLastRowQF()
  2. ' 定义源工作表(无需激活)
  3. Dim sourceSheet As Worksheet
  4. Set sourceSheet = ThisWorkbook.Worksheets("Steam Data")
  5. ' 查找特定列中的最后一行
  6. Dim lastRow As Long
  7. lastRow = sourceSheet.Range("A" & sourceSheet.Rows.Count).End(xlUp).Row
  8. ' 根据列和最后一行设置源范围
  9. Dim sourceRange As Range
  10. Set sourceRange = sourceSheet.Range("A" & lastRow & ":S" & lastRow)
  11. Dim lCell As Range
  12. Set lCell = sourceRange.Cells(1)
  13. Dim destRange As Range
  14. Set destRange = sourceRange.Offset(1)
  15. Application.ScreenUpdating = False
  16. Dim x As Long
  17. For x = lCell.Value + 1 To sourceSheet.Range("A1").Value
  18. sourceRange.Copy destRange
  19. Set destRange = destRange.Offset(1)
  20. Next x
  21. Application.ScreenUpdating = True
  22. End Sub

改进版本

  1. Sub CopyLastRow()
  2. Const DATE_COLUMN As Long = 1
  3. With ThisWorkbook.Worksheets("Steam Data").Columns("A:S")
  4. Dim lCell As Range: Set lCell = .Columns(DATE_COLUMN) _
  5. .Find("*", , xlFormulas, , , xlPrevious)
  6. If lCell Is Nothing Then Exit Sub ' 列中没有数据
  7. Dim rCount As Long: rCount = .Cells(DATE_COLUMN).Value - lCell.Value
  8. If rCount < 1 Then
  9. MsgBox "行已经复制。", vbExclamation
  10. Exit Sub
  11. End If
  12. Dim srg As Range: Set srg = Intersect(lCell.EntireRow, .Cells)
  13. Dim drg As Range: Set drg = srg.Offset(1).Resize(rCount)
  14. srg.Copy drg
  15. End With
  16. End Sub
英文:

Copy (Append) Last Row

如何复制并粘贴最后一行直到特定日期?

A Quick Fix (Preserving the Idea)

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

  1. Sub CopyLastRowQF()
  2. &#39; Define the source sheet (no need to activate it)
  3. Dim sourceSheet As Worksheet
  4. Set sourceSheet = ThisWorkbook.Worksheets(&quot;Steam Data&quot;)
  5. &#39; Find the last row in a specific column
  6. Dim lastRow As Long
  7. lastRow = sourceSheet.Range(&quot;A&quot; &amp; sourceSheet.Rows.Count).End(xlUp).Row
  8. &#39; Set the source range according to columns and last row
  9. Dim sourceRange As Range
  10. Set sourceRange = sourceSheet.Range(&quot;A&quot; &amp; lastRow &amp; &quot;:S&quot; &amp; lastRow)
  11. Dim lCell As Range
  12. Set lCell = sourceRange.Cells(1)
  13. Dim destRange As Range
  14. Set destRange = sourceRange.Offset(1)
  15. Application.ScreenUpdating = False
  16. Dim x As Long
  17. For x = lCell.Value + 1 To sourceSheet.Range(&quot;A1&quot;).Value
  18. sourceRange.Copy destRange
  19. Set destRange = destRange.Offset(1)
  20. Next x
  21. Application.ScreenUpdating = True
  22. End Sub

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

An Improvement

  1. Sub CopyLastRow()
  2. Const DATE_COLUMN As Long = 1
  3. With ThisWorkbook.Worksheets(&quot;Steam Data&quot;).Columns(&quot;A:S&quot;)
  4. Dim lCell As Range: Set lCell = .Columns(DATE_COLUMN) _
  5. .Find(&quot;*&quot;, , xlFormulas, , , xlPrevious)
  6. If lCell Is Nothing Then Exit Sub &#39; no data in column
  7. Dim rCount As Long: rCount = .Cells(DATE_COLUMN).Value - lCell.Value
  8. If rCount &lt; 1 Then
  9. MsgBox &quot;Rows already copied.&quot;, vbExclamation
  10. Exit Sub
  11. End If
  12. Dim srg As Range: Set srg = Intersect(lCell.EntireRow, .Cells)
  13. Dim drg As Range: Set drg = srg.Offset(1).Resize(rCount)
  14. srg.Copy drg
  15. End With
  16. End Sub

答案3

得分: 0

以下是翻译好的代码部分:

  1. 最后部分的代码需要进行调整,移除不正确构造的 `For ... Next` 循环以及 `If ... Else ... End If`。请参考我的代码片段:
  2. Dim x As Integer
  3. Dim lcell As Long
  4. lcell = Cells(Rows.Count, 1).End(xlUp).Row
  5. ' For x = lcell To 14 'x = Range("A1")
  6. ' If x < Range("A1") Then
  7. sourceRange.Copy
  8. sourceRange.Offset(1).PasteSpecial
  9. ' Else
  10. ' End If
  11. ' Next x

请注意,我已经按照您的要求只返回了翻译好的部分。

英文:

The last part of your code needs to be adjusted by removing the For ... Next loop (which is improperly constructed to begin with) and removing the If ... Else ... End If. See my code snippet below:

  1. Dim x As Integer
  2. Dim lcell As Long
  3. lcell = Cells(Rows.Count, 1).End(xlUp).Row
  4. &#39; For x = lcell To 14 &#39;x = Range(&quot;A1&quot;)
  5. &#39; If x &lt; Range(&quot;A1&quot;) Then
  6. sourceRange.Copy
  7. sourceRange.Offset(1).PasteSpecial
  8. &#39; Else
  9. &#39; End If
  10. &#39; Next x

huangapple
  • 本文由 发表于 2023年6月30日 02:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583920.html
匿名

发表评论

匿名网友

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

确定