我想在x的行数中选择最后8行(行数每月更改),这里已经有一个线程,但仅适用于一列。我在几列中都需要它,并尝试了两种不同的方法,但是由于语法的原因,它们都不起作用。

Sheets("Sheet2").Select
LastRow = Range("D" & Rows.Count).End(xlUp).Row
Set Last8Rows =  Range("A" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8aRows = Range("B" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8bRows = Range("C" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8cRows = Range("D" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8dRows = Range("E" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8eRows = Range("F" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8fRows = Range("G" & LastRow).Offset(-7, 0).Resize(8, 1)
LastxRows = Last8Rows + Last8aRows + Last8bRows + Last8cRows + Last8dRows + Last8eRows + Last8fRows
LastxRows.Copy


我第二次尝试

Sheets("Sheet2").Select
LastRow = Range("D" & Rows.Count).End(xlUp).Row
Set Last8Rows =  Range("A:D" & LastRow).Offset(-7, 0).Resize(8, 1)
Last8Rows.Copy

最佳答案

尝试下面的代码(无需Select即可复制工作表):

Option Explicit

Sub Copy_LastEight_Rows()

Dim Sht2            As Worksheet
Dim LastRow         As Long
Dim Last8Rows       As Range

Set Sht2 = ThisWorkbook.Sheets("Sheet2")

LastRow = Sht2.Range("D" & Sht2.Rows.Count).End(xlUp).Row

' modify Column D to your need
Set Last8Rows = Sht2.Range("A" & LastRow - 7 & ":D" & LastRow)
Last8Rows.Copy

End Sub

关于excel - 在Excel Vba中选择最后8行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38693039/

10-09 05:53