Select range between 3rd and 2nd to last row Runti

2019-09-15 11:22发布

I'm trying to write a code that first find the last row, and selects all rows between the 2nd to last row and the 3rd row. Then proceeds to delete them. But I keep running into an error 13 : Type mismatch

  Dim StartRow, LastRow, NuRow As Variant
  StartRow = 3
  Sheets("Sheet3").Activate
  If WorksheetFunction.CountA(Cells) > 0 Then

    'Search for any entry, by searching backwards by Rows.

   LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows,  SearchDirection:=xlPrevious).Row
End If
    NuRow = LastRow - 1
    Rows("StartRow:NuRow").Delete 'Run time error 13 Type Mismatch

Any ideas ?

2条回答
乱世女痞
2楼-- · 2019-09-15 11:46

It is the Rows object that is throwing the error. It is expecting a row Index in the form "3:20" (for example). You are passing it a string "StartRow:NuRow".

Try changing that statement to:

Rows(StartRow & ":" & NuRow).Delete
查看更多
We Are One
3楼-- · 2019-09-15 11:55

Try this instead:

Rows(StartRow & ":" & NuRow).Delete
查看更多
登录 后发表回答