Delete all rows after last used row

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
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
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hello , I am providing you the different way of the above procedure.
But I would Like to inform you that if your previous code already inputs some unwanted data in any cells , then those cells are also considered in the used range.
However, I think you can figure out a way to involve the code I am providing with your already existing code to solve your problem.
Please provide a feedback if this helps.
VBA Code:
Sub d()
Dim lastblank As Long, bottomrow As Long

lastblank = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1
bottomrow = Sheets(1).Cells(Rows.Count, 1).Row
Range("A" & lastblank & ":" & "A" & bottomrow).Delete

End Sub
 
Upvote 0
Hello , I am providing you the different way of the above procedure.
But I would Like to inform you that if your previous code already inputs some unwanted data in any cells , then those cells are also considered in the used range.
However, I think you can figure out a way to involve the code I am providing with your already existing code to solve your problem.
Please provide a feedback if this helps.
VBA Code:
Sub d()
Dim lastblank As Long, bottomrow As Long

lastblank = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1
bottomrow = Sheets(1).Cells(Rows.Count, 1).Row
Range("A" & lastblank & ":" & "A" & bottomrow).Delete

End Sub
Doesn't seem to be working. if I put random data in say 10 rows down in column C it isn't deleted it
 
Upvote 0
Doesn't seem to be working. if I put random data in say 10 rows down in column C it isn't deleted it
It wouldn't delete, because the formula

cells(rows.count,1).endxl(up).row

Or

Used range

Will ensure that all the cells which have the data are covered and only empty cells are left behind.

I would suggest that you could mention the rows that have the unwanted data and purposely delete it.
Or else rectify or Modify your previous code which is giving the unwanted data.

You can also share your previous code here. I am sure some expert will provide a solution to correct the code for you, so that you get rid of the unwanted data
 
Upvote 0
H
Doesn't seem to be working. if I put random data in say 10 rows down in column C it isn't deleted it
Hello, Please ignore my previous reply, I tried solving it againg using current region and it worked for my set of assumed data. Please try this once and provide a feedback

VBA Code:
Sub delete()
Dim row As Long

row = Range("a:a").CurrentRegion.End(xlDown).row
Range("a" & row + 1, "a" & Cells(Rows.Count, 1).End(xlUp).row - 1).delete

End Sub
 
Last edited:
Upvote 0
It wouldn't delete, because the formula

cells(rows.count,1).endxl(up).row

Or

Used range

Will ensure that all the cells which have the data are covered and only empty cells are left behind.

I would suggest that you could mention the rows that have the unwanted data and purposely delete it.
Or else rectify or Modify your previous code which is giving the unwanted data.

You can also share your previous code here. I am sure some expert will provide a solution to correct the code for you, so that you get rid of the unwanted data
Ok I will test it out. I mean i wanted a way to delete unwanted rows not being used so it could still work. ill try it on my actual dataset on monday. thank you
 
Upvote 0
Ok I will test it out. I mean i wanted a way to delete unwanted rows not being used so it could still work. ill try it on my actual dataset on monday. thank you
Okay. The last code which I gave should work for you I suppose. If there is or are empty cells between your actual data and unwanted data.
Thank you. Do let us know what is the outcome of the code.
 
Upvote 0
Try using the Find method to determine bottomrow
VBA Code:
Sub deleteit()
    Dim lastblank As Long, bottomrow As Long

    lastblank = Workbooks("Compare").Sheets("Periodic").Cells(Rows.Count, 1).End(xlUp).row + 1
    bottomrow = Workbooks("Compare").Sheets("Periodic").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).row
   
    Workbooks("Compare").Sheets("Periodic").Rows(lastblank & ":" & bottomrow).Delete
End Sub
 
Upvote 0
Okay. The last code which I gave should work for you I suppose. If there is or are empty cells between your actual data and unwanted data.
Thank you. Do let us know what is the outcome of the code.
it appears to be shifting the cells to the left and not deleting all the empty rows
 
Upvote 0
Try using the Find method to determine bottomrow
VBA Code:
Sub deleteit()
    Dim lastblank As Long, bottomrow As Long

    lastblank = Workbooks("Compare").Sheets("Periodic").Cells(Rows.Count, 1).End(xlUp).row + 1
    bottomrow = Workbooks("Compare").Sheets("Periodic").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).row
  
    Workbooks("Compare").Sheets("Periodic").Rows(lastblank & ":" & bottomrow).Delete
End Sub
It doesn't seem to be deleting all the unused rows. My data in this examples ends on row 68 (could change) and i am looking to delete row 69 to the end. if i do it manually it works. trying to limit the size of my doc with unnecessary rows its at 85,000kb when i remove empty rows manually it goes to 1,500kb
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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