Quicker VBA needed

Dimbers

New Member
Joined
Sep 20, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Evening,

I have a VBA code that works as it should but is a little clunky to use, as in its quite slow.

The code looks in column A and if it finds "Live" it will hide all of those rows. I have the same code running to look for "Complete" and will then hide those rows.

It would be great to have a code that runs quicker if anyone can offer some help?

Below is the code I'm using




VBA Code:
Sub HideRowsLive()

Application.ScreenUpdating = False

 Dim cell As Range

  For Each cell In ActiveWorkbook.ActiveSheet.Columns("A").Cells

      If cell.Value = "Complete" Then
      
       cell.EntireRow.Hidden = False
       
       End If
  
  Next cell
  
  For Each cell In ActiveWorkbook.ActiveSheet.Columns("A").Cells

       
       If cell.Value = "Live" Then
      
       cell.EntireRow.Hidden = False
       
       End If

  Next cell
  

  For Each cell In ActiveWorkbook.ActiveSheet.Columns("A").Cells

      If cell.Value = "Live" Then

      cell.EntireRow.Hidden = True
      

    End If

  Next cell
 
sorry, fell foul of the 10 min rule & neglected to include parameter for the different worksheets

VBA Code:
Sub HideRows(ByVal sh As Object, ByVal UserAction As HideLiveComplete)
    Dim c               As Range, HideRange As Range, DataRange As Range
    Dim HideWhat        As String
    
    With sh
        .Unprotect
        'unhide all rows
        .Cells.Rows.Hidden = False
        If UserAction = ShowAll Then Exit Sub
        'set data range
        Set DataRange = .Cells(1, 1).Resize(.Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
    
    HideWhat = Choose(UserAction, "LIVE", "COMPLETE")
    
    For Each c In DataRange.Cells
        'create union
        If UCase(c.Value) = HideWhat Then
            If HideRange Is Nothing Then
                Set HideRange = c
            Else
                Set HideRange = Union(HideRange, c)
            End If
        End If
    Next c
    'hide rows in one go
    If Not HideRange Is Nothing Then HideRange.EntireRow.Hidden = True
End Sub

to call code pass the worksheet object as first argument

VBA Code:
HideRows Worksheets("Template1"), HideLive

If using ActiveX commandbuttons on your worksheet you should be able to use the ME keyword in place of Worksheets("Template1")

Dave
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,215,111
Messages
6,123,159
Members
449,098
Latest member
Doanvanhieu

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