Delete Unused Rows

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have some files that may only have a used range of 1000 rows for example. But when I scroll down it goes down to the maximum amount of rows. Is there a code that I can run that will delete unused rows as it takes quite some time when doing formulas or a 'find' for example.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I have some files that may only have a used range of 1000 rows for example. But when I scroll down it goes down to the maximum amount of rows. Is there a code that I can run that will delete unused rows as it takes quite some time when doing formulas or a 'find' for example.
VBA Code:
Sub Delete_unusedcells()
' Delete_unusedcells Macro
    Cells.Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.Delete Shift:=xlUp
End Sub

Try this. If not working, share your sheet. I can fix that.
 
Upvote 0
VBA Code:
Sub Delete_unusedcells()
' Delete_unusedcells Macro
    Cells.Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.Delete Shift:=xlUp
End Sub

Try this. If not working, share your sheet. I can fix that.
Thanks but that didn't work. Unfortunately I cannot share file as it contains sensitive data.
 
Upvote 0
I have some files that may only have a used range of 1000 rows for example. But when I scroll down it goes down to the maximum amount of rows. Is there a code that I can run that will delete unused rows as it takes quite some time when doing formulas or a 'find' for example.
What do you mean when you say Used Range ?
Do you mean visually there are only a 1000 rows or do you mean in VBA code similar to Activesheet.usedrange.address is picking up a much larger area than you are expecting ?
Do you have formulas that go right down to the bottom of the sheet but are returning "" and are you happy to delete these rows ?
 
Upvote 0
Just to keep things moving, try this on a COPY of your workbook.

VBA Code:
Sub DeleteRowsBelowData()

    Dim usedRng As Range
    Dim lastRow As Long, usedRngLastRow
    Dim sht As Worksheet
    
    Set sht = ActiveSheet

    Set usedRng = sht.UsedRange
    usedRngLastRow = usedRng.Rows.Count + usedRng.Cells(1, 1).Row - 1
    
    lastRow = sht.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious).Row

    sht.Rows(lastRow + 1).Resize(usedRngLastRow - lastRow).Delete
    
    ' Reset the New Used Range
    Dim ForResetOnly As String
    ForResetOnly = sht.UsedRange.Address

End Sub
 
Upvote 0
What do you mean when you say Used Range ?
Do you mean visually there are only a 1000 rows or do you mean in VBA code similar to Activesheet.usedrange.address is picking up a much larger area than you are expecting ?
Do you have formulas that go right down to the bottom of the sheet but are returning "" and are you happy to delete these rows ?
What I mean is I may have data going down to row 10000, but the sheet goes down to maybe 500000 or more. So thousands of empty rows slowing things down.
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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