[excel] How do I delete everything below row X in VBA/Excel?

I have a long variable X that contains a number. Say it's 415.

How do I delete everything on the worksheet from Row 415 and below?

I want to ensure my spreadsheet is clean in Row 415 and anything else that might be below it.

How do I do this? Thanks.

This question is related to excel vba

The answer is


Another option is Sheet1.Rows(x & ":" & Sheet1.Rows.Count).ClearContents (or .Clear). The reason you might want to use this method instead of .Delete is because any cells with dependencies in the deleted range (e.g. formulas that refer to those cells, even if empty) will end up showing #REF. This method will preserve formula references to the cleared cells.


Any Reference to 'Row' should use 'long' not 'integer' else it will overflow if the spreadsheet has a lot of data.


This function will clear the sheet data starting from specified row and column :

Sub ClearWKSData(wksCur As Worksheet, iFirstRow As Integer, iFirstCol As Integer)

Dim iUsedCols As Integer
Dim iUsedRows As Integer

iUsedRows = wksCur.UsedRange.Row + wksCur.UsedRange.Rows.Count - 1
iUsedCols = wksCur.UsedRange.Column + wksCur.UsedRange.Columns.Count - 1

If iUsedRows > iFirstRow And iUsedCols > iFirstCol Then
    wksCur.Range(wksCur.Cells(iFirstRow, iFirstCol), wksCur.Cells(iUsedRows, iUsedCols)).Clear
End If

End Sub