引用不同工作表的最后一行

目前我在Sheet2上有这个

Range("A2").Select Selection.AutoFill Destination:=Range("A2:A1001") Range("A2:A1001").Select Range("B2").Select Selection.AutoFill Destination:=Range("B2:B1001") Range("B2:B1001").Select Range("C2").Select Selection.AutoFill Destination:=Range("C2:C1001") Range("C2:C1001").Select Range("D2").Select Selection.AutoFill Destination:=Range("D2:D1001") Range("D2:D1001").Select Range("E2").Select Selection.AutoFill Destination:=Range("E2:E1001") Range("E2:E1001").Select Range("G2").Select Selection.AutoFill Destination:=Range("G2:G1001") Range("G2:G1001").Select Range("H2").Select Selection.AutoFill Destination:=Range("H2:H1001") Range("H2:H1001").Select Range("I2").Select Selection.AutoFill Destination:=Range("I2:I1001") Range("I2:I1001").Select Range("J2").Select Selection.AutoFill Destination:=Range("J2:J1001") Range("J2:J1001").Select Range("K2").Select Selection.AutoFill Destination:=Range("K2:K1001") Range("K2:K1001").Select 

而不是目标范围A2到A1001我想有A2到最后一行作为sheet1例如,如果在sheet1中最后一行是147行我想要代码填充Selection.AutoFill Destination:=Range("A2:A147")

我不知道如何做到这一点。

谢谢

这应该适合你…

 Sub LastRowAutofill() On Error Resume Next Dim wsSheet1 As Worksheet: Set wsSheet1 = Worksheets("Sheet1") Dim LastRow As Long LastRow = wsSheet1.Columns(1).Find("*", LookIn:=xlValues, SearchDirection:=xlPrevious).Row Dim i As Long With Worksheets("Sheet2") For i = 1 To 11 If i <> 6 Then .Cells(2, i).AutoFill Destination:=.Range(.Cells(2, i), .Cells(LastRow, i)) Next i End With End Sub 

或者,如果您想要整个sheet1中的最后一行,则可以使用以下内容:

  LastRow = wsSheet1.UsedRange.Find("*", LookIn:=xlValues, SearchDirection:=xlPrevious).Row 

假设您的原始数据在Sheet1上,而您正在复制到Sheet2,那么您将如何执行此操作:

 Dim intLastrow As Integer intLastrow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row Sheets("Sheet2").Range("A2").Select Selection.AutoFill Destination:=Range(cells(2,"A"),cells(intLastrow, "A")) Sheets("Sheet2").Range(Cells(2, "A"), Cells(intLastrow, "A")).Select intLastrow = Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row Sheets("Sheet2").Range("B2").Select Selection.AutoFill Destination:=Range(cells(2,"B"),cells(intLastrow, "B")) Sheets("Sheet2").Range(Cells(2, "B"), Cells(intLastrow, "B")).Select