Hi there. I am trying to do the opposite of deleting all visible rows after an autofilter. Basically, I want the user to be able to filter to a department and have the macro delete all of the not visible rows leaving only the data for their filtered department. I currently have the following code. It runs without error, but it doesn't delete the rows. Any help would be appreciated!
Code:
Option Explicit
Sub Dept_Only()
Dim oRow As Range, rng As Range
Dim myRows As Range
Dim ws As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With ActiveSheet
Set myRows = Intersect(Range("ReadyAll[Name]").EntireRow, .UsedRange)
If myRows Is Nothing Then Exit Sub
End With
For Each oRow In myRows.Columns(2).Cells
If oRow.EntireRow.Hidden Then
If rng Is Nothing Then
Set rng = oRow
Else
Set rng = Union(rng, oRow)
End If
End If
Next
Set ws = Sheets("Readiness_All")
If rng.Rows.Count = 1 Then
ws.Rows(rng.Row & ":" & rng.Row).Deletes
ElseIf rng Is Nothing Then
rng.EntireRow.Delete
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub