Macro running slow

Robert1985

New Member
Joined
Jul 6, 2017
Messages
3
Hi,

I have created a macro (below) to search a range and hide rows with no value, however, on running the macro it takes around a minute to run.
Is there anyway this can be sped up?

Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("D1:D105")
If xRg.Value = "" Then
xRg.EntireRow.Hidden = True

Else
xRg.EntireRow.Hidden = False
End If
Next

End Sub


Thanks,
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
See if this is any quicker:

Code:
Dim xRg As Range, rng As Range, rngHide As Range, rngVis As Range
Dim arr, myVal as Long, i As Long

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

Set rng = Range("D1:D105")
myVal = rng.Cells(1).Row
arr = rng
  
For i = LBound(arr, 1) To UBound(arr, 1)
    Select Case Len(arr(i, 1))
        Case 0
            If Not rngHide Is Nothing Then
                Set rngHide = Union(rngHide, Rows(i + myVal - 1))
            Else
                Set rngHide = Rows(i + myVal - 1)
            End If
        Case Is > 0
            If Not rngVis Is Nothing Then
                Set rngVis = Union(rngVis, Rows(i + myVal - 1))
            Else
                Set rngVis = Rows(i + myVal - 1)
            End If
    End Select
Next
If Not rngHide Is Nothing Then rngHide.EntireRow.Hidden = True
If Not rngVis Is Nothing Then rngVis.EntireRow.Hidden = False

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

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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