Delete all rows after last used row

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
779
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
 
What happens with
VBA Code:
Sub LoseThatWeight()

    Dim x As Long, LastRow As Long, LastCol As Long

    Application.ScreenUpdating = False

        With Workbooks("Compare").Sheets("Periodic")

            LastRow = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            LastCol = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
            .Range(.Cells(1, LastCol + 1), .Cells(Rows.Count, Columns.Count)).Delete
            .Range(.Cells(LastRow + 1, 1), .Cells(Rows.Count, Columns.Count)).Delete
        End With

    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
What happens with
VBA Code:
Sub LoseThatWeight()

    Dim x As Long, LastRow As Long, LastCol As Long

    Application.ScreenUpdating = False

        With Workbooks("Compare").Sheets("Periodic")

            LastRow = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            LastCol = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
            .Range(.Cells(1, LastCol + 1), .Cells(Rows.Count, Columns.Count)).Delete
            .Range(.Cells(LastRow + 1, 1), .Cells(Rows.Count, Columns.Count)).Delete
        End With

   
    Application.ScreenUpdating = True
End Sub
it errors on this

.Range(.Cells(1, LastCol + 1), .Cells(rows.count, Columns.count)).Delete
 
Upvote 0
That code as posted should not delete all your data. This line
VBA Code:
LastRow = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
finds the last used cell in all columns.
This line goes from 1 line below the above
Rich (BB code):
 .Range(.Cells(LastRow + 1, 1), .Cells(Rows.Count, Columns.Count)).Delete

Please post your workbook on a free file hosting site like www.box.com or www.dropbox.com. Mark the file for sharing and paste the link it provides in the thread.
Make sure that you amend any sensitive data before sharing.
 
Upvote 0
That code as posted should not delete all your data. This line
VBA Code:
LastRow = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
finds the last used cell in all columns.
This line goes from 1 line below the above
Rich (BB code):
 .Range(.Cells(LastRow + 1, 1), .Cells(Rows.Count, Columns.Count)).Delete

Please post your workbook on a free file hosting site like www.box.com or www.dropbox.com. Mark the file for sharing and paste the link it provides in the thread.
Make sure that you amend any sensitive data before sharing.
unfortunately I cannot share it has sensitive user information. is it because my data doesn't start until Row A13 and then could end anywhere. so A1:A12 is blank
 
Upvote 0
A1:A12 being blank makes no difference to that code.
 
Upvote 0
A1:A12 being blank makes no difference to that code.
Before:
1602427395499.png


After Code:
1602427435077.png


VBA Code:
    Application.ScreenUpdating = False

        With Sheets("Compare")

            LastRow = .Cells.Find(What:="*", After:=.Range("B1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
            LastCol = .Cells.Find(What:="*", After:=.Range("B1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
            .Range(.Cells(1, LastCol + 1), .Cells(rows.count, Columns.count)).Delete
            .Range(.Cells(LastRow + 1, 1), .Cells(rows.count, Columns.count)).Delete
        End With

    Application.ScreenUpdating = True
 
Upvote 0
No you have changed A1 to B1, do not do this.
Rich (BB code):
After:=.Range("B1")
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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