Hi! I'm a Noob with VBA, but I've been trying to automate some processes at work. We have an excel file that contains some 70k records, which we download every day. However, we only need records that don't have a cancelled or completed status (Statuses are text in a column). When I manually autofilter to de-include those statuses, I end up with about 6k records of usable data, which is fine, but i wrote a macro to automatically filter the file for those cancelled or completed statuses, select the rows, and delete them. The end result looks about right, but the file takes MUCH longer to open and interact with than the 5k records could be responsible for. Also, when I ask the Immediate window in VBA for a Used Range count, it still includes the deleted records. I've enclosed my code and I hope I'm clear about the trouble.
(The Rows still show Blue after the Macro runs, but if I manually Select and RightClick->Delete the rows in question, they go away permanent-like.)
Sub NewQDeleteCompletedCancelled()
Dim NewQSheet As Worksheet
Dim DeleteValue1, DeleteValue2 As String
Dim rng As Range
Dim calcmode As Long
Workbooks.Open Filename:="U:\Test\NewQ.xls"
With Application
calcmode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
DeleteValue1 = "Cancelled"
DeleteValue2 = "Completed"
With ActiveSheet
.AutoFilterMode = False
'Range is the column where the Status of the record lives
.Range("V1:V" & .Rows.Count).AutoFilter Field:=1, _
Criteria1:=DeleteValue1, Operator:=xlOr, Criteria2:=DeleteValue2
With .AutoFilter.Range
On Error Resume Next
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then rng.EntireRow.Delete
End With
.AutoFilterMode = False
End With
With Application
.ScreenUpdating = True
.Calculation = calcmode
.DisplayAlerts = False
End With
ActiveWorkbook.Save
Application.DisplayAlerts = True
End Sub
Any help you can provide would be simply neato!
Thanks!
Bookmarks