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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,224,830
Messages
6,181,225
Members
453,025
Latest member
Hannah_Pham93

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