VBA Code stops executing, but doesn't throw errors

thborng

New Member
Joined
Nov 6, 2013
Messages
8
Hi everyone!

I'm writing some code to delete hidden rows in a spreadsheet, and it is not working...

The worksheet I'm trying it on has a Table (the kind with [@ColumnName] references), which I filtered to show only some rows that match a condition. The others are, of course, hidden. If I run the code on this worksheet, it just STOPS executing after the first time it runs the "ActiveSheet.rows(rowNum).EntireRow.Delete" line. It doesn't even get to the "End If" instruction. It just stops!

There's two things that puzzle me: first, that it actually DOES delete the row just before it stops executing; second, that it takes a few seconds to execute that instruction, so I guess something is going wrong there.

Here's the code:
Code:
Sub DeleteHiddenRows()
    Dim rowNum As Integer
    rowNum = ActiveSheet.UsedRange.rows.Count
    Application.ScreenUpdating = False
    For i = rowNum To 2 Step -1
        If ActiveSheet.rows(i).EntireRow.Hidden Then
            ActiveSheet.rows(i).EntireRow.Delete
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

Please help!

P.S.: I've also tried this code with a simple worksheet only containing values from 1 to 15 in cells A1 to A15, and hiding rows 3,4,5,7,9 and 12, and it works like a charm... My guess is the problem lies with the Table.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
It actually worked now... All I had to do was to disable automatic calculation during the execution of my code. The final working code is:

Code:
Sub DeleteHiddenRows()
    Dim rowNum As Integer
    rowNum = ActiveSheet.UsedRange.rows.Count
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    For i = rowNum To 2 Step -1
        If ActiveSheet.rows(i).EntireRow.Hidden Then
            ActiveSheet.rows(i).EntireRow.Delete
        End If
    Next i
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,143
Members
449,098
Latest member
Doanvanhieu

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