0

I need the VBA to take the values from column A in the "Final_Sheet" and filter by those values in column A of the "SIS_Case_Contacts" sheet.

Sub filter()
Dim lastRowSISCaseContacts As Long
Dim lastRowFinalSheet As Long
Dim ws As Worksheet
Set ws = Sheets("SIS_Case_Contacts")

With Sheets("SIS_Case_Contacts")
    lastRowSISCaseContacts = .Range("A" & .Rows.Count).End(xlUp).Row
End With

With Sheets("Final_Sheet")
    lastRowFinalSheet = .Range("A" & .Rows.Count).End(xlUp).Row
End With

    ws.Range("A2:A" & lastRowSISCaseContacts).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
    Sheets("Final_Sheet").Range("A2:A" & lastRowFinalSheet), Unique:=False
End Sub

This gives me a Run-time error '1004': Advancedfilter method of Range class failed.

Jay
  • 65
  • 6

1 Answers1

0

AdvancedFilter rather than AutoFilter will allow you to use the values from one sheet as the criteria to filter another sheet with.

Something like this could work:

    Dim lastRowContacts As Long
    Dim lastRowFinalSheet As Long

    With Sheets("Contacts")
        lastRowContacts = .Range("A" & .Rows.Count).End(xlUp).Row
    End With

    With Sheets("Final_Sheet")
        lastRowFinalSheet = .Range("A" & .Rows.Count).End(xlUp).Row
    End With

    ws.Range("A2:A" & lastRowContacts).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
            Sheets("Final_Sheet").Range("A2:A" & lastRowFinalSheet), Unique:=False
Matt
  • 38
  • 6
  • This looks really good, do you know what could be happening when I run this and it gives me a Run-time error '424'? – Jay Apr 13 '20 at 21:00
  • It means that an object referred to in the code wasn't found. You will still need the `Dim ws...` and `Set ws...` lines of your code. If you have already included those, are the sheet names correct? – Matt Apr 13 '20 at 21:04
  • I updated the my original code question above. And am now getting the rang of object error. – Jay Apr 15 '20 at 13:18