Slow macro in 2007 - runs fast in 2003

dlhecht

New Member
Joined
Oct 6, 2009
Messages
2
This code takes 40 seconds to run in 2007 but <1 second in excel 2003. Why? How can I make it run faster? Thank you in advance!

For RowCount = 1 To 1800
If ActiveCell.Value = 0 Then
ActiveCell.EntireRow.Hidden = True
ActiveCell.Offset(1, 0).Activate
Else
ActiveCell.Offset(1, 0).Activate
End If
Next RowCount
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
This code takes 40 seconds to run in 2007 but <1 second in excel 2003. Why? How can I make it run faster? Thank you in advance!

For RowCount = 1 To 1800
If ActiveCell.Value = 0 Then
ActiveCell.EntireRow.Hidden = True
ActiveCell.Offset(1, 0).Activate
Else
ActiveCell.Offset(1, 0).Activate
End If
Next RowCount
Welcome to the MrExcel board!

I'm not sure why that would take so much longer in Excel 2007 than in Excel 2003. However, there should be faster ways in both versions. It is generally not required to actually select cells to work with them and selecting slows your code considerably. Also, there is often an alternative to looping through every row that is much faster than the looping process.

I don't know where your ActiveCell is when this code is triggerred, or exactly what your goal is. However, as an example, if you are looking to hide zero value rows by looking down column C, and you have a heading in row 1, then you could achieve the result quickly using AutoFilter with something like this:
Code:
With Range("C1", Range("C" & Rows.Count).End(xlUp))
    .AutoFilter Field:=1, Criteria1:="<>0"
End With

If you need it to also hide blank value rows, then change the middle line to
Code:
.AutoFilter Field:=1, Criteria1:="<>0", Operator:=xlAnd, Criteria2:="<>"

Finally, I think you meant to post your code with indentations and that is a good idea. To do that you need to use code tags. Click the 'Quote' button at the bottom of my post and you should be able to see how that is done.

If you need more help, post back with more details of just what you have in your sheet and what you are trying to do.
 
Upvote 0
Peter,

Thank you very much for such a fast reply. The goal of the current macro is to accomplish the following:

"Within a range of rows, look down a column and hide any row whose value is either zero or blank"

Which, I think, is exactly what you are suggesting. I can't wait to try your suggestion out. I'll let you know if it works.

-David
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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