VBA - code to delete all rows below last row with data

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a problem with my code. I need to delete all rows below last row with data in column B. Code below works, but I need to adjust it to delete all rows with no data in column B, but starting from row 10.
So it skips rows 1-9 (column B rows 1-9 contains blank cells). Any ideas how to improve it?

VBA Code:
Sub PricingRemoveRows()


Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long

Set WS = ThisWorkbook.ActiveSheet

With WS
    Set LastCell = .Cells(.Rows.Count, "B").End(xlUp)
    LastCellRowNumber = LastCell.Row
    Rows(LastCellRowNumber + 1 & ":" & Rows.Count).Delete
End With

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
VBA Code:
Sub Kra()
   Dim UsdRws As Long
   
   UsdRws = Range("B" & Rows.Count).End(xlUp).Offset(1).Row
   If UsdRws < 11 Then UsdRws = 11
   Rows(UsdRws & ":" & Rows.Count).Delete
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub Kra()
   Dim UsdRws As Long
  
   UsdRws = Range("B" & Rows.Count).End(xlUp).Offset(1).Row
   If UsdRws < 11 Then UsdRws = 11
   Rows(UsdRws & ":" & Rows.Count).Delete
End Sub

Amazing, works like a charm! Thank you!
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top