从LastRow到LastRow偏移VBA

时间:2019-01-11 09:05:43

标签: excel vba

我正在尝试从最后一行到下面一行执行AutoFill。因此LastRowLastRow Offset (1)

我知道如何在VBA中找到最后一行,但是有些我找不到解决此难题的方法。

我认为它将类似于以下内容:

LastRow = Cells(Rows.Count, 2).End(xlUp).Row
LastRow2 = Cells(Rows.Count, 2).End(xlUp).Offset(1)


Range("B" & LastRow : "AA" & LastRow).Select
Range("B" & LastRow2 : "AA" & LastRow2).Autofill

如果不清楚,请询问,我们将不胜感激

3 个答案:

答案 0 :(得分:1)

在您的.Row变量的末尾添加LastRow2

LastRow2 = Cells(Rows.Count, 2).End(xlUp).Offset(1).Row

答案 1 :(得分:1)

在您的.Row变量的末尾添加LastRow2,并正确使用Range.AutoFill method

SourceRange.Autofill Destination:=DestinationRange

DestinationRange 必须包括SourceRange

我强烈推荐avoid using Select

LastRow = Cells(Rows.Count, 2).End(xlUp).Row

Range("B" & LastRow : "AA" & LastRow).Autofill Destination:=Range("B" & LastRow : "AA" & LastRow + 1)
                                                                        '^ Note this must be LastRow not LastRow + 1!

替代:

Dim LastCell As Range
Set LastCell = Cells(Rows.Count, 2).End(xlUp) 'without .Row

LastCell.Resize(1, 26).AutoFill Destination:=LastCell.Resize(2, 26) '1 down

替代:

Dim SourceRange As Range
Set SourceRange = Cells(Rows.Count, 2).End(xlUp).Resize(1, 26) 'results in column B to AA

SourceRange.AutoFill Destination:=SourceRange.Resize(RowSize:=2) '1 down

答案 2 :(得分:1)

可能会这样声明自动填充的目标范围。

With ActiveSheet
    Set Rng = .Cells(.Rows.Count, "B").End(xlUp).Resize(2, 26)
End With
相关问题