Excel VBA - 如何清除动态范围(从标题下方到最后一行)

时间:2014-10-02 14:40:29

标签: excel vba excel-vba

我有一个贷款摊还计划,根据期间创建摊销计划,如何在再次运行贷款摊销之前清除范围?例如,如果我运行它10年,然后运行5年,6-10年仍然显示,因为它们尚未被清除。

这是我的代码:

outRow = 3
With Sheets("Loan")
lCol = .Cells(3, .Columns.Count).End(xlToLeft).Column
lrow = .Cells(.Rows.Count, lCol).End(xlUp).Row
End With

Sheets("Loan").Range(Cells(outRow, 5), Cells(Lastrow, lastCol)).ClearContents

3 个答案:

答案 0 :(得分:2)

试试这个,将工作表的UsedRange偏移1行,并清除内容

With Sheets("Loan").UsedRange
        .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).ClearContents
End With

答案 1 :(得分:2)

如果你添加" + 1"之后。如果文件为空,您将保留标题。 [如果它是空的,代码运行时没有" + 1"它将删除标题]

Dim Lastrow As Long
Lastrow = Range("A" & Rows.Count).End(xlUp).Row +1
Range("A2:A" & Lastrow).Clear

答案 2 :(得分:1)

Dim Lastrow As Long
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:A" & Lastrow).Clear

以上内容适用于清除A列中的所有单元格。如果您需要多列,则更改A行中的第二个Range(A2:A