Macro is slow when hiding rows when cell has specific value

bisel

Board Regular
Joined
Jan 4, 2010
Messages
233
Office Version
  1. 365
Platform
  1. Windows
I have a macro that runs when a sheet is activated. It is working OK ... but I need some help.

Here is the macro code for hiding rows when a cell in the row has a specific value.

VBA Code:
:
:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

For Each c In Range("compdetails_includes").Cells
    If c.Value = 0 Then
        c.EntireRow.Hidden = True
    Else: c.EntireRow.Hidden = False
    End If
  Next
:
:

In my case, the cell with have a value of either 0 or 1. If zero, then hide the entire row. I could easily change the value to True or False and then do a boolean check for value.

There are 2500 rows in the above range. This is working OK, but I am doing some mods and I find that if I copy a cell from another sheet into this sheet and do a paste special, copy picture link, instead of less than second to run the worksheet activate macro it takes over 7 seconds. So two questions for the group:
  1. Why when I do a cell copy and then paste it as a picture link does that routine for hiding rows slow to a crawl?
  2. Can anyone recommend a technique to speed up the hiding of an entire row based on the value in the cell?
Steve
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Why when I do a cell copy and then paste it as a picture link does that routine for hiding rows slow to a crawl?
No idea.

Can anyone recommend a technique to speed up the hiding of an entire row based on the value in the cell?

Try this ...
VBA Code:
    Dim rHid As Range
    For Each c In Range("compdetails_includes").Cells
        If c.Value = 0 Then
            If rHid Is Nothing Then
                Set rHid = c
            Else
                Set rHid = Application.Union(rHid, c)
            End If
        End If
    Next
    Range("compdetails_includes").EntireRow.Hidden = False
    rHid.EntireRow.Hidden = True
 
Upvote 0
Solution
No idea.



Try this ...
VBA Code:
    Dim rHid As Range
    For Each c In Range("compdetails_includes").Cells
        If c.Value = 0 Then
            If rHid Is Nothing Then
                Set rHid = c
            Else
                Set rHid = Application.Union(rHid, c)
            End If
        End If
    Next
    Range("compdetails_includes").EntireRow.Hidden = False
    rHid.EntireRow.Hidden = True

Very nice. That addresses my issues. Works very rapidly.

Thanks,

Steve
 
Upvote 0
You are welcome and thanks for letting me know (y)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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