How can I speed this up?

davlcam

New Member
Joined
Aug 4, 2009
Messages
45
This runs really slow. Any ideas on how to speed it up - its a toggle, when the values change from 0 to 1, the cells will hide / unhide. But it runs painfully slow.

Private Sub test99()

Application.ScreenUpdating = False

For Each Cell In Range("B5:B100")
If Cell.Value = 0 Then
Cell.EntireRow.Hidden = True
' End If
Else
' If Cell.Value > 0 Then
Cell.EntireRow.Hidden = False
End If
Next Cell

Application.ScreenUpdating = True

End Sub
 
Late, tired and not paying attention, try:
Rich (BB code):
Sub PiPiForMy ()


Dim i As Long, j As Long
Dim iRange As Range

With Application
    .ScreenUpdating = False
    .Calculation = xlManual
End With

If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False

j = Range("B5:B100").Find(what:="0").Row
Set iRange = Range("B5:B100").Find(what:="0")

For i = j + 1 To 100
    If Range("B" & i) = 0 Then
        Set iRange = Union(iRange, Range("B" & i))
    End If
Next i

iRange.EntireRow.Hidden = True

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With


End Sub
See bits in red where I've changed my code

Bam! Microseconds. Thank you sir.

" I stand here tonight as excited as a masochist who has just been arrested by the Spanish Inquisition. " ~ Black Adder
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Glad it helped. Made a slight change to include a variable, k that you can change if you want values other than 0 (see bits in red)
Rich (BB code):
Sub FillEmpty()

Dim ws As Worksheet
Dim i As Long, j As Long, k As Long
Dim msg As String
Dim iRange As Range

With Application
    .ScreenUpdating = False
    .Calculation = xlManual
End With

k = 0

If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
Cells.EntireRow.Hidden = False

j = Range("B3:B100").Find(what:=k).Row
Set iRange = Range("B3:B100").Find(what:=k)

For i = j + 1 To 100
    If Range("B" & i) = k Then
        Set iRange = Union(iRange, Range("B" & i))
    End If
Next i

iRange.EntireRow.Hidden = True

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
 
Upvote 0
On second review, noticed 2 things.

Cell B5 with a value of 0 does not hide.

Thinking

For i= j+1 to 100

Should actually be

For i=j+ 0 to 100

For my test data -

In Cell B22 and B23, I have positive values, with the cells hidden- before i run the code. The code does not unhide them. This simulates the scenario where: the previous lookup used to set the value of the B cells showed a 0 in B22 and B23, but the new lookup has resulted in non zero values now in those cells, and the code should now unhide those cells.
 
Last edited:
Upvote 0
The line in blue should be unhiding all the rows on the sheet before it searches the rows in column B for the ones that contain 0 that you want to hide. Press F8 to step through the code and see if B22 and B23 are unhidden (or not)

Anyway, I've set up a second union range for values that are not 0 and then put in code to set any rows of this second union to not be hidden

Also just change

For i = j + 1 to 100
to
For i = j to 100

Updated:
Rich (BB code):
Sub FillEmpty()

Dim ws As Worksheet
Dim i As Long, j As Long, k As Long
Dim msg As String
Dim iRange As Range, jRange As Range

With Application
    .ScreenUpdating = False
    .Calculation = xlManual
End With

k = 0

If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
Cells.EntireRow.Hidden = False

j = Range("B3:B100").Find(what:=k).Row
Set iRange = Range("B3:B100").Find(what:=k)
Set jRange = Range("B1")

For i = j To 100
    If Range("B" & i) = k Then
        Set iRange = Union(iRange, Range("B" & i))
    Else
        Set jRange = Union(jRange, Range("B" & i))
    End If
Next i

iRange.EntireRow.Hidden = True
jRange.EntireRow.Hidden = False

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
 
Last edited:
Upvote 0
That last bit got it.

I saw your unhide all line, but for some reason on the F8 step through it didn't do anything.

Works fine now tho. Thanks again.

Good bit of code to archive possibly. Ive seen a few posts on best way to hide unhide rows when a value is zero, (where the cells referenced are dynamic) and this is by far the fastest i've seen.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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