VBA running slow any suggestions??

dimples_21188

New Member
Joined
Aug 21, 2017
Messages
12
Please could someone help me with the code below, it runs very slow, is there a way to speed it up? Thank you

VBA Code:
Sub HideUnusedRowsInventory()
Dim LR As Long, i As Long
'LR = Range("b" & Rows.Count).End(xlUp).Row
LR = 1468
For i = 129 To 1468
Rows(i).Hidden = Range("b" & i).Value = 0
Next i
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
See if this quicker

VBA Code:
Sub HideUnusedRowsInventory()

Dim FR As Long, LR As Long, arrRng As Range, arr, rng As Range, i As Long

Application.ScreenUpdating = False

'LR = Range("b" & Rows.Count).End(xlUp).Row
FR = 129
LR = 1468

Set arrRng = Range(Cells(FR, 2), Cells(LR, 2))
arrRng.EntireRow.Hidden = False
arr = arrRng
For i = LBound(arr) To UBound(arr)
    If Len(arr(i, 1)) > 0 And arr(i, 1) = 0 Then
        If Not rng Is Nothing Then
            Set rng = Union(rng, Cells(i + FR - 1, 2))
        Else
            Set rng = Cells(i + FR - 1, 2)
        End If
    End If
Next
If Not rng Is Nothing Then rng.EntireRow.Hidden = True

Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
See if this quicker

VBA Code:
Sub HideUnusedRowsInventory()

Dim FR As Long, LR As Long, arrRng As Range, arr, rng As Range, i As Long

Application.ScreenUpdating = False

'LR = Range("b" & Rows.Count).End(xlUp).Row
FR = 129
LR = 1468

Set arrRng = Range(Cells(FR, 2), Cells(LR, 2))
arrRng.EntireRow.Hidden = False
arr = arrRng
For i = LBound(arr) To UBound(arr)
    If Len(arr(i, 1)) > 0 And arr(i, 1) = 0 Then
        If Not rng Is Nothing Then
            Set rng = Union(rng, Cells(i + FR - 1, 2))
        Else
            Set rng = Cells(i + FR - 1, 2)
        End If
    End If
Next
If Not rng Is Nothing Then rng.EntireRow.Hidden = True

Application.ScreenUpdating = True
   
End Sub
You sir are a genius thank you
 
Upvote 0
What do you mean by run on the sheet?
I would ideally like the code you wrote to run on a worksheet automatically. I already have a Private Sub Worksheet_Change(ByVal Target As Range) running on the worksheet, I didn't know if I could simply add the code underneath? however would this then make my spreadsheet run slow because it would always be running?
 
Upvote 0
This would do it if you are just changing one cell at a time.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim FR As Long, LR As Long, arrRng As Range, arr, rng As Range, i As Long

Application.ScreenUpdating = False

'LR = Range("b" & Rows.Count).End(xlUp).Row
FR = 129
LR = 1468

Set arrRng = Range(Cells(FR, 2), Cells(LR, 2))

If Target.Cells.Count = 1 Then
    If Not Intersect(Target, arrRng) Is Nothing Then
        arrRng.EntireRow.Hidden = False
        arr = arrRng
        For i = LBound(arr) To UBound(arr)
            If Len(arr(i, 1)) > 0 And arr(i, 1) = 0 Then
                If Not rng Is Nothing Then
                    Set rng = Union(rng, Cells(i + FR - 1, 2))
                Else
                    Set rng = Cells(i + FR - 1, 2)
                End If
            End If
        Next
        If Not rng Is Nothing Then rng.EntireRow.Hidden = True
    End If
End If

Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Thinking about it you had it looping through loads of cells. If you want it to just use a worksheet change then you can make it simpler by just testing 'target'.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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