0

I need a little help. Let's start with the code:

Range("a1").AutoFilter Field:=1, Criteria1:="W1793-PAS (Agency)"
'exclude 1st row (titles)
With Intersect(Range("a1").CurrentRegion, _
               Range("2:60000")).SpecialCells(xlCellTypeVisible)
    .Rows.Delete
End With
ActiveSheet.ShowAllData
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False

I need to create a conditional statement for the above code that will stop the procedure if there is no text matching Criteria1 (W1793-PAS (Agency)). I don't want this to run if there is no rows with W1793-PAS (Agency) in it.

1 Answer 1

1

Simply check if the Autofilter is returning anything or not by storing the filtered range in a Range object and then delete it

Dim rng As Range

'Remove any filters
ActiveSheet.AutoFilterMode = False

With Range("A1")
  .AutoFilter Field:=1, Criteria1:="W1793-PAS (Agency)"
  '~~> Set this here
  Set rng = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With

'Remove any filters
ActiveSheet.AutoFilterMode = False

'~~> Check if there were any filtered results and then delete
If Not rng Is Nothing Then rng.Delete
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.