This VBA Code takes 3 minutes to execute. Need alternative.

Ioweyouone

New Member
Joined
Aug 14, 2009
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hello,

The objective is to hide rows based on the value in a cell. I wrote a formula for all cells in the range M141:M1500, that returns a 1 if I want that row hidden. =COUNTA(D141:I141)+IF(M140>2,1,0)
The attached image shows the Code I wrote to hide all the rows with a 1 value in column M. It works, but it takes over 3 minutes to hide the rows after I execute the command. Any suggestions to speed this up?

vba.jpg
 
First, I recommend that when you want to share code you paste the code directly into your post, instead of taking a picture of the code. After you paste, select the code and click the VBA button to apply code tags.

Before the loop add
VBA Code:
Application.ScreenUpdating = False
and after the loop add
VBA Code:
Application.ScreenUpdating = True

That will help but if it's not enough let us know. There might be a couple of other tricks, like building the rows into one range and hiding them all at once.
 
Upvote 0
This works but I haven't done any performance testing.
VBA Code:
Private Sub HideThem()

   Dim Cell As Range
   Dim WholeRange As Range
      
   Application.ScreenUpdating = False
   
   For Each Cell In Range("A1:A32")
      If Cell.Value = 1 Then
         If WholeRange Is Nothing Then
            Set WholeRange = Cell
         Else
            Set WholeRange = Union(WholeRange, Cell)
         End If
      End If
   Next Cell
   WholeRange.EntireRow.Hidden = True

   Application.ScreenUpdating = True

End Sub
 
Upvote 0
maybe just a filter code would do the charm?

Range("$M$141:$M$1499").Select
Selection.AutoFilter
ActiveSheet.Range("$M$141:$M$1499").AutoFilter Field:=1, Criteria1:="=1", _
Operator:=xlAnd
 
Upvote 0
Shouldn't that be "<>1" if you want that row hidden using AutoFilter (the range should also start in M140 as autofilter requires a header)
VBA Code:
Sub Test()
ActiveSheet.Range("M140:M1499").AutoFilter 1, "<>1"
End Sub
 
Last edited:
Upvote 0
Solution

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