|
Folks:
Can you help? Array filtering works but no data gets deleted. If you know how to fix this way let me know.
I went with a simpler method and it worked for me, but I like how the array method knew how to determine the proper filter range, and make a variable out of the last row (something I was asking about for another macro).
I'll post my latest easier method code,but here are the details of my last problem:
LAST PROBLEMS DETAILS:
http://www.rondebruin.nl/delete.htm
Solution from above link won't work in my file but works in test file from link. When I modified the test file, adding spaces, replacing the terms with my terms in the Sheet and then modifying the code. (ALso, filtering doesn't seem to be case sensitive when I step through):
Code: myArr = Array("*Cost*", "*cost*", "*Total*", "*total*", "")
and
Code: .Range("C3:C" & .Rows.Count).AutoFilter Field:=1, Criteria1:=myArr(I)
In the test file no problem, but my file, I step through it and watch the filters working but no rows are deleted. I saved the test file as an Excel 2010 xlsm file same as mine and it still worked.
Now, another forum user offered this delete code:
Sub FilteredRowsDeleteCode()
Range("a4", Selection.SpecialCells(xlCellTypeLastCell)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.EntireRow.Delete
End Sub
I stepped thru the other array code and stopped it when it got one of the array terms filtered, then ran the Sub FilteredRowsDeleteCode() and it worked, but I can't seem to join it with the other code, and get it inside the Active Sheet With statement, maybe because that statement defines ranges already so there is a conflict. I tried calling the separate macro but still no go.
Here is the Full modified test file code:
Sub Delete_with_Autofilter_Arrayx()
Dim rng As Range
Dim calcmode As Long
Dim myArr As Variant
Dim I As Long
With Application
calcmode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'Fill in the values that you want to delete
myArr = Array("*Cost*", "*cost*", "*Total*", "*total*", "")
For I = LBound(myArr) To UBound(myArr)
'Sheet with the data, you can also use Sheets("MySheet")
With ActiveSheet
'Firstly, remove the AutoFilter
.AutoFilterMode = False
'Apply the filter
.Range("C3:C" & .Rows.Count).AutoFilter Field:=1, Criteria1:=myArr(I)
Set rng = Nothing
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
'Remove the AutoFilter
.AutoFilterMode = False
End With
Next I
With Application
.ScreenUpdating = True
.Calculation = calcmode
End With
End Sub
Thanks, Rowland
|