The below code worked 10 minutes ago and now I get error 1004: Autofilter method of Range class failed. Any idea why? I want to filter column C based on letter J - so filter only the values starting with J. After,I want to delete the hidden rows, but this doesn’t seem to work.please help
Option Explicit
Dim ws As Worksheet
Dim filterarray As Variant
Dim findarray As Variant
Dim MyLastRow1 As Long, i As Long, j As Long, k As Long
Sub test23()
Set ws = ThisWorkbook.Worksheets("T&E Report")
MyLastRow1 = Range("C100000").End(xlUp).Row
findarray = Array("J")
ReDim filterarray(0 To 0)
j = 0
For k = 0 To UBound(findarray)
For i = 2 To MyLastRow1
If InStr(ws.Cells(i, 3).Value, findarray(k)) > 0 Then
filterarray(j) = ws.Cells(i, 3).Value
j = j + 1
ReDim Preserve filterarray(0 To j)
End If
Next i
Next k
ws.Range("$C$1:$C$" & MyLastRow1).AutoFilter Field:=3, Criteria1:=Array(filterarray), Operator:=xlFilterValues
End sub
Dimstatements need to be within yourSub. If they are global you need to usePublicto declare.Dim, they are global but only known within the module. If you declare them asGlobal, they are visible to all modules of your project."J*"as the criterion.