I currently have a worksheet with approximately 40K rows and 108 columns that I am working with. What I am doing is creating a list that when user selects a particular text in that list then certain columns and/or rows are hidden.
The issue I am encountering is that when 'Unsecured Streamline' is selected, the code to hide the rows takes a REALLY long time to run. Is there a more efficient way to execute what I am trying to achieve here?
Here is the portion of the code I am referring to:
ElseIf Target.Text = "UNSECURED 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 something else I have tried that works but still takes far too long:
For Each cell In Intersect(Me.UsedRange, Me.Columns("R"))
If cell.Value <> "AP" Then
cell.EntireRow.Hidden = True
End If
Next cell
Here is the full 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:O,AG:AQ,AX:AY,BA:BA,BC:BD,BF:BH,BJ:BK,BP:BQ,BT:BT,BW:BZ,CA:CB,CC:CI,CK:CK,CQ:CQ,CT:DD").EntireColumn.Hidden = True
ElseIf Target.Text = "RATES" Then
Range("A:DD").EntireColumn.Hidden = False
Range("A:C,J:O,P:P,AB:AE,AG:AG,AR:AW,BE:BF,BW:CB,CE:CI,CK:CN,CR:CS,DB:DD").EntireColumn.Hidden = True
ElseIf Target.Text = "UNSECURED 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
I think one issue may be that I am hiding each row individually but I do not know how to solve for this. Any assistance would be greatly appreciated since, as you can probably tell, I am quite new to VBA.