VBA Code Hiding Rows Optimization

Brandon81

New Member
Joined
Jan 24, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am using a drop down list in Excel in which user select a text and then columns and/or rows are being hidden. The data that I am using is large, approx. 39K rows. I think that because of this, when "Streamline" is selected, the columns are being hidden and so are rows, but it takes an extremely long time for all the rows to hide. What I am trying to achieve is that when user selects "Streamline" then certain columns are hidden in addition to rows that do not contain "AP" in column 'R'.

So my question is: Is there a way to optimize the hiding of rows when user selects "Streamline" other than what I have done? Is there a better way?

This is the part of my code that I am questioning:
VBA Code:
ElseIf Target.Text = "STREAMLINE" Then
            Range("A:DD").EntireColumn.Hidden = False
            Range("AA:AM").EntireColumn.Hidden = True
            For Each cell In ActiveWorkbook.ActiveSheet.Columns("R").Cells
                If cell.Value <> "AP" Then
                cell.EntireRow.Hidden = True
                End If
            Next cell

Here is the entirety of what I have in VBA thus far:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = ("$D$1") Then
        If Target.Text = "ALL" Then
            Range("A:DD").EntireColumn.Hidden = False
    ElseIf Target.Text = "OVERRIDES" Then
            Range("A:DD").EntireColumn.Hidden = False
            Range("A:C,J:L,N:O,AH:AQ,AX:AY,BF:BH,BJ:BK,BP:BQ,BT:BT,BW:BZ,CA:CB,CC:CI,CK:CK,CT:DD").EntireColumn.Hidden = True
     ElseIf Target.Text = "RATES" Then
            Range("A:DD").EntireColumn.Hidden = False
            Range("J:K,N:O,P:P,AR:AW,BE:BF,CK:CN,CR:CS").EntireColumn.Hidden = True
    ElseIf Target.Text = "STREAMLINE" Then
            Range("A:DD").EntireColumn.Hidden = False
            Range("AA:AM").EntireColumn.Hidden = True
            For Each cell In ActiveWorkbook.ActiveSheet.Columns("R").Cells
                If cell.Value <> "AP" Then
                cell.EntireRow.Hidden = True
                End If
            Next cell
        End If
    End If
End Sub
 
Last edited by a moderator:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi & welcome to MrExcel.
How about
VBA Code:
            For Each cell In Intersect(Me.UsedRange, Me.Columns("R"))
                If cell.Value <> "AP" Then
                cell.EntireRow.Hidden = True
                End If
            Next cell
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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