Delete hidden rows *runs slowly* [VBA]

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am using this code to copy data from workbook_data and paste it into workbook_master (see below).

Then I filter out all unnecessary data.

Then I delete all hidden rows (the rows I filtered out in previous step).

Now, the problem is that it takes a while to complete the last step (see the code below -> 'Delete hidden rows)

Would it be possible to somehow speed up the last part?

Thank you.

VBA Code:
'Open workbook_data
Workbooks.Open ("address")

'Copy data from workbook_data, from worksheet data
Workbooks("workbook_data.xlsx").Sheets("data").Range("A1", Range("H" & Rows.count).End(xlUp)).Copy

'Paste copied data inside workbook_master, inside worksheet master
Workbooks("workbook_master.xlsm").Sheets("master").Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

'Close workbook_data without save
On Error Resume Next
Workbooks("workbook_data.xlsx").Close savechanges:=False
On Error GoTo 0

'Workbook_master with sheet master is now activated
'Filter the header in the range A1 based on values in the cells G4, H4 and I4 in the worksheet Sheet_X
ActiveSheet.Range("A1").AutoFilter Field:=1, Criteria1:=Array(Worksheets("Sheet_X").Range("G4").Value, Worksheets("Sheet_X").Range("H4").Value, Worksheets("Sheet_X").Range("I4").Value), Operator:=xlFilterValues

'Delete hidden rows
Dim sht As Worksheet
Dim lastrow
Set sht = ActiveSheet
lastrow = sht.UsedRange.Rows(sht.UsedRange.Rows.count).Row
For i = lastrow To 1 Step -1
If Rows(i).Hidden = True Then Rows(i).EntireRow.DELETE
Next
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
try this on a copy of your file.
VBA Code:
Dim lastrow
Set sht = ActiveSheet
lastrow = sht.UsedRange.Rows(sht.UsedRange.Rows.count).Row

With Range("a1", Range("a" & lastrow)
    .SpecialCells(12).Select
    .Cells.EntireRow.Hidden = False
    Selection.EntireRow.Hidden = True
    .SpecialCells(12).EntireRow.Delete
    .Cells.EntireRow.Hidden = False
End With
 
Upvote 0
Hi rpaulson,

so I have repaced the last part of my code with yours (see below) and I got an error "No cells were found." with this line lighlighted. The code deleted basically every line in my worksheet.
VBA Code:
.SpecialCells(12).EntireRow.DELETE

Full code here?
VBA Code:
'Open workbook_data
Workbooks.Open ("address")

'Copy data from workbook_data, from worksheet data
Workbooks("workbook_data.xlsx").Sheets("data").Range("A1", Range("H" & Rows.count).End(xlUp)).Copy

'Paste copied data inside workbook_master, inside worksheet master
Workbooks("workbook_master.xlsm").Sheets("master").Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

'Close workbook_data without save
On Error Resume Next
Workbooks("workbook_data.xlsx").Close savechanges:=False
On Error GoTo 0

'Workbook_master with sheet master is now activated
'Filter the header in the range A1 based on values in the cells G4, H4 and I4 in the worksheet Sheet_X
ActiveSheet.Range("A1").AutoFilter Field:=1, Criteria1:=Array(Worksheets("Sheet_X").Range("G4").Value, Worksheets("Sheet_X").Range("H4").Value, Worksheets("Sheet_X").Range("I4").Value), Operator:=xlFilterValues

'rpaulson
Dim lastrow
Set sht = ActiveSheet
lastrow = sht.UsedRange.Rows(sht.UsedRange.Rows.count).Row

With Range("a1", Range("a" & lastrow))
    .SpecialCells(12).Select
    .Cells.EntireRow.Hidden = False
    Selection.EntireRow.Hidden = True
    .SpecialCells(12).EntireRow.Delete
    .Cells.EntireRow.Hidden = False
End With
 
Upvote 0
Tried using the regular speed gaining code?

VBA Code:
Application.EnableEvents = True/False
Application.ScreenUpdating = True/False
Application.Calculation = xlCalculationManual/xlManualCalculation
 
Upvote 0
Tried using the regular speed gaining code?

VBA Code:
Application.EnableEvents = True/False
Application.ScreenUpdating = True/False
Application.Calculation = xlCalculationManual/xlManualCalculation
Yes sir. I have it all at the start of my procedure.
 
Upvote 0
oops need to error trap if no hidden rows. otherwise it will hide all rows

VBA Code:
On Error Resume Next

Dim lastrow
Set sht = ActiveSheet
lastrow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row

With Range("a1", Range("a" & lastrow))
    .SpecialCells(12).Select
    .Cells.EntireRow.Hidden = False
    Selection.EntireRow.Hidden = True
    .SpecialCells(12).EntireRow.Delete
    .Cells.EntireRow.Hidden = False
End With

On Error GoTo 0
 
Upvote 0
My belief is if you:

1- Use Advanced Filtering of the data to copy to a dummy sheet
2- Delete every data row in one line
3- Paste back the range from dummy

would be fastest.
 
Upvote 0
You've probably already tried this, but if those other things aren't working, then restarting Excel or your CPU may be required.
 
Upvote 0
@Martin_H
I wrote a code to answer to a similar problem which is how to delete hidden rows fast.
Check these:

note:
deleting rows on a large non-contiguous range is slow, so it's better just to clear the content first and then sort data to "remove" the blank rows.
 
Upvote 0
You could also consider applying the filter to the source workbook and only copying the visible rows to the destination workbook, removing the need to delete anything.
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,125,002
Members
449,202
Latest member
Pertotal

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