Delete all rows after last used row

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
770
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I found this VBA code on this site. but it doesn't work. and was hoping if someone could assist me as to why

I am looking to delete all rows below the last row of data in column A. I noticed my excel creates many unused rows after my other code runs.
VBA Code:
    Dim bottomrow, lastblank As Long

bottomrow = Workbooks("Compare").Sheets("Periodic").UsedRange.rows.count
lastblank = Workbooks("Compare").Sheets("Periodic").Cells(rows.count, 1).End(xlUp).row + 1
  
Range("A" & lastblank & "A" & bottomrow).EntireRow.Delete

1602264331050.png
 
Then you have something strange in your cells.
The After parameter in Find is for doing the lookup, basically when using XLPrevious it looks at the cell before A1 which is the last cell on the sheet and then it looks backwards from there, changing it to B1 changes the start point of the search.
What happens with
VBA Code:
Sub LoseThatWeight()

    Dim x As Long, LastRow As Long

    Application.ScreenUpdating = False

    With Sheets("Compare")
        LastRow = .Columns(1).Find("*", , xlValues, , xlByRows, xlPrevious).row
        .Rows(LastRow + 1 & ":" & Rows.Count).delete
    End With
   
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Then you have something strange in your cells.
The After parameter in Find is for doing the lookup, basically when using XLPrevious it looks at the cell before A1 which is the last cell on the sheet and then it looks backwards from there, changing it to B1 changes the start point of the search.
What happens with
VBA Code:
Sub LoseThatWeight()

    Dim x As Long, LastRow As Long

    Application.ScreenUpdating = False

    With Sheets("Compare")
        LastRow = .Columns(1).Find("*", , xlValues, , xlByRows, xlPrevious).row
        .Rows(LastRow + 1 & ":" & Rows.Count).delete
    End With
  
    Application.ScreenUpdating = True
End Sub
Lean, mean and fighting machine now :cool:
 
Upvote 0

Forum statistics

Threads
1,214,540
Messages
6,120,106
Members
448,945
Latest member
Vmanchoppy

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