Excel VBA – 如何清除dynamic范围(从下面的标题,到最后一行)

我有一个贷款的摊销时间表,根据期间创造摊销时间表,我怎么能清除范围,在再次运行贷款摊销? 例如,如果我运行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 

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

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

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

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

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