Hello,
I'm new to VBA programming, and I'm hoping that someone can help me to pinpoint the root cause of an error message I'm getting. The error message is:
Runtime Error 1004: AutoFilter method of Range class failed
I'm trying to duplicate the code of another programmer on a different worksheet. I've pasted the code below and highlighted in red the line of code that is resulting in the error. I've also included the Function code in case there is something I'm missing there.
Sub FilterFilledData()
Dim tmpRng As Range
Application.EnableCancelKey = xlDisabled
strActPred = LCase(Worksheets("Filled").Range("B4"))
strPos = LCase(Worksheets("Filled").Range("B8"))
strRegion = LCase(Worksheets("Filled").Range("B5"))
strArea = LCase(Worksheets("Filled").Range("B6"))
strDistrict = LCase(Worksheets("Filled").Range("B7"))
'On Error Resume Next
Worksheets("FilledDetail").Cells.Clear
'If Err.Number > 0 Then
'Worksheets.Add
'ActiveSheet.Name = "FilledDetail"
'End If
On Error GoTo 0
Set tmpRng = Worksheets("SCData").Cells(1, 1).CurrentRegion
Worksheets("SCData").AutoFilterMode = False
tmpRng.AutoFilter
If strRegion <> "all" Then
strfind = "Region"
colHeading = FindFilledColumnHeadings(strfind)
tmpRng.AutoFilter Field:=colHeading, Criteria1:=strRegion
End If
If strArea <> "all" Then
strfind = "Area"
colHeading = FindFilledColumnHeadings(strfind)
tmpRng.AutoFilter Field:=colHeading, Criteria1:=strArea
End If
If strDistrict <> "all" Then
strfind = "DSL"
colHeading = FindFilledColumnHeadings(strfind)
tmpRng.AutoFilter Field:=colHeading, Criteria1:=strDistrict
End If
If strActPred <> "all" Then
strfind = "PredOrActual"
colHeading = FindFilledColumnHeadings(strfind)
tmpRng.AutoFilter Field:=colHeading, Criteria1:=strActPred
End If
If strPos <> "all" Then
strfind = "Position Simplified"
colHeading = FindFilledColumnHeadings(strfind)
tmpRng.AutoFilter Field:=colHeading, Criteria1:=strPos
End If
tmpRng.SpecialCells(xlCellTypeVisible).Copy Worksheets("FilledDetail").Range("A1")
Worksheets("FilledDetail").Columns("AH:AR").Delete
Worksheets("FilledDetail").Columns("AE").Delete
Set tmpRng = Nothing
End Sub
Function FindFilledColumnHeadings(ByVal strHeading As String) As Integer
Dim tmpSht As Worksheet
Set tmpSht = Worksheets("SCData")
For i = 1 To tmpSht.Cells(1, 1).End(xlToRight).Column
If Trim(strHeading) = tmpSht.Cells(1, i) Then
FindFilledColumnHeadings = i
Set tmpSht = Nothing
Exit Function
End If
Next
FindFilledColumnHeadings = 0
Set tmpSht = Nothing
End Function
Any help anyone can provide is greatly appreciated! I apologize that I may not have pasted the code in a visually-friendly format. This is my first time posting a thread, so hopefully I've provided all the necessary details.
Thanks!
Jen
Bookmarks