Excel VBA - How to clear a dynamic range (from bel

2019-09-08 01:45发布

I have a loan amortization schedule that creates the amortization schedule depending on the periods, how can I clear the range, before running the loan amortization again? For example, if I run it for a 10 year period, then run it for a 5 year period, years 6-10 still show up because they haven't been cleared.

Here is my code:

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条回答
再贱就再见
2楼-- · 2019-09-08 02:10

If you add "+1" after .Row you will keep the headings in case the file is empty. [if it is empty and the code runs without "+1" it will erase the headings]

Dim Lastrow As Long
Lastrow = Range("A" & Rows.Count).End(xlUp).Row +1
Range("A2:A" & Lastrow).Clear
查看更多
一夜七次
3楼-- · 2019-09-08 02:19
Dim Lastrow As Long
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:A" & Lastrow).Clear

The above will work for clearing all cells in column A. If you need it for multiple columns then change the second A in the Range(A2:A line.

查看更多
成全新的幸福
4楼-- · 2019-09-08 02:23

Try this, takes the UsedRange of the worksheet offset 1 row down to and clears contents

With Sheets("Loan").UsedRange
        .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).ClearContents
End With
查看更多
登录 后发表回答