清除lastRow下面的特定范围(不要删除行)

时间:2018-01-29 08:10:59

标签: vba

您能否建议如何清除A列的lastRow以下的特定范围?

所以,我当前的代码“删除行”

.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(.UsedRange.Rows.Count, 1).EntireRow.Delete

我需要的是[清除] A列中lastRow下方的数据矩形。因此代码需要转到A列中最后一个填充单元格下方的单元格并清除B:V的矩形+ 10级以下(我不想删除行)。

由于

2 个答案:

答案 0 :(得分:1)

要删除单元格的内容,请使用Range方法clearContents 要删除格式,请使用方法clear

Const NumberOfRowsToBeCleared = 10
Dim r As Range, lastRow As Long, maxRow As Long, ws As Worksheet
Set ws = ActiveSheet    ' Assign to the worksheet you want to deal with
With ws
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row

    ' This version clears 10 rows
    Set r = .Range("B" & lastRow + 1 & ":V" & lastRow + NumberOfRowsToBeCleared+1)
    ' Alternative way to define the range
    Set r = .Range(.Cells(lastRow + 1, 2), .Cells(lastRow + NumberOfRowsToBeCleared+1, 22))

    ' This version clears until the end of the sheet
    maxRow = .UsedRange.Rows.Count
    Set r = .Range("B" & lastRow + 1 & ":V" & maxRow + 11)
    ' Again alternative way to define the range
    Set r = .Range(.Cells(lastRow + 1, 2), .Cells(maxRow, 22))

    r.ClearContents  ' Clear contents
    '  r.Clear  ' Clear contents and formatting

End With

答案 1 :(得分:0)

试试这个。

'find last populated cell in column A
Set last = .Range("A:A").Find("*", Cells(1, 1), searchdirection:=xlPrevious)
'range last.row + 1 to last.row + 1 + 10 from columns B:V clear
.Range(.Cells(last.Row + 1, 2), .Cells(last.Row + 11, 22)).Clear

找到行并清除定义的范围