Ark68
Well-known Member
- Joined
- Mar 23, 2004
- Messages
- 4,435
- Office Version
-
- 365
- 2016
- Platform
-
- Windows
Below is a snippet of code (graciously assisted by Colin_L and VoG) I use to delete rows of a database isolated by an advanced filter.
It works greats when there are rows that meet the criteria. Although it hasn't happened yet, it is inevitable that at some point the filter will result in no rows needing to be deleted. I will receive an error no doubt when it tries to delete visual cells that are not there.
What error protection method can I build into this code to prevent a break from occuring? I have 5 instances of similar code part of the entire routine each filtering and deleting different sets of criteria, so uncertain whether or not protection can be global or for each separate filter attempt.
Jenn
It works greats when there are rows that meet the criteria. Although it hasn't happened yet, it is inevitable that at some point the filter will result in no rows needing to be deleted. I will receive an error no doubt when it tries to delete visual cells that are not there.
What error protection method can I build into this code to prevent a break from occuring? I have 5 instances of similar code part of the entire routine each filtering and deleting different sets of criteria, so uncertain whether or not protection can be global or for each separate filter attempt.
Code:
' *** Delete CLASS C facilities ***
'
With Worksheets("Fields_Regular")
Dim iFCCdel As Integer
With .Range("A1:K6000")
.AdvancedFilter _
Action:=xlFilterInPlace, _
CriteriaRange:=Worksheets("Criteria").Range("G16:G17"), _
Unique:=False
iFCCdel = WorksheetFunction.CountIf(Columns("A"), "C")
MsgBox "CLASS C RECORDS: " & iFCCdel
On Error Resume Next
Set rngToDelete = .Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
If .FilterMode Then .ShowAllData
End With
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
'
Exit Sub
ErrorHandler:
MsgBox Err.Number & vbLf & Err.Description
Resume ErrorExit
Jenn