Is there a "go faster" option to hide rows

Dav1d

New Member
Joined
Sep 27, 2010
Messages
27
I'm working with a form that can have varying rows of data. There will come a time when I want to archive them according to a criteria ("3" in Column A). The code I'm using is below

My problem is its going really, really slow, even when working through 10 or so lines. Is there a tip that might speed things up?

Sub NewArchive()
For Each cell In ActiveSheet.Range("A17:A500")
If cell.Value = "3" Then cell.EntireRow.Hidden = True
Next
End Sub



Many thanks guys!
David
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Don't hide them one at a time. Combine each cell with a 3 in the range and then do an .entirerow.hidden = true on that range.

Code:
Sub NewArchive()
    Dim threeRange As Range, cell As Range
    For Each cell In ActiveSheet.Range("A17:A500")
        If cell.Value = "3" Then
            If threeRange Is Nothing Then
                Set threeRange = cell
            Else
                Set threeRange = Union(threeRange, cell)
            End If
        End If
    Next
    If Not threeRange Is Nothing Then threeRange.EntireRow.Hidden = True
End Sub
Having said that, there's not much wrong with filtering. Maybe you could use the advanced filter instead if you don't want the listboxes to appear.
 
Last edited:
Upvote 0
Weaver, many thanks. I think you've given me the fish and taught me to fish too!
There may be a faster way to do what you want. What is in Column A generating your numbers (the 3 in particular)... constants or formulas? What is the maximum number of rows your data could possibly have (I'm not looking for an exact number, just a ball-park amount... 100, 1000, 10000, etc.)?
 
Upvote 0
There may be a faster way to do what you want. What is in Column A generating your numbers (the 3 in particular)... constants or formulas? What is the maximum number of rows your data could possibly have (I'm not looking for an exact number, just a ball-park amount... 100, 1000, 10000, etc.)?


Hi Rick

Its a formula: an If statement checking several criteria. Ballpark is circa 500

Thank you
 
Upvote 0
If your headers are in row 16 (as I'm guessing), here's a method using advanced filter, which might be faster than the previous version for larger datasets.

Code:
Sub Macro1()
    Dim ws, cs, fr
    Application.ScreenUpdating = False
    Set ws = ActiveSheet
    Set fr = Range("A16", Cells(Rows.Count, "A").End(xlUp))
    Set cs = Sheets.Add
    Range("A1") = ws.Range("A16")
    Range("A2") = "<>3"
    Set cr = cs.Range("A1:A2")
    fr.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=cr, Unique:=False
    Application.DisplayAlerts = False
    cs.Delete
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Give this code a try...

Code:
Sub HideRowsWith3InColumnA()
  Dim LastRow As Long, UnusedColumn As Long
  Const StartRow = 17
  UnusedColumn = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Column + 1
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  With Cells(StartRow, UnusedColumn).Resize(LastRow - StartRow + 1)
    .FormulaR1C1 = "=IF(RC1=3,""X"","""")"
    .Value = .Value
    .SpecialCells(xlCellTypeConstants).EntireRow.Hidden = True
    .Clear
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,572
Members
452,927
Latest member
whitfieldcraig

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