Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VBA to autofilter Multiple columns, with values from different sheet

I want to use VBA to filter a dump-sheet by 2 columns, with criteria gotten from values on a different sheet in the same workbook.

Used code is:

Sub FilterOnCellValue()

With Sheets("Dump")
    .Range("A1:Z10000").AutoFilter Field:=9, Criteria1:=Sheets("ControlPlanning").Range("C1").Value, Field:=23, Criteria1:=Sheets("ControlPlanning").Range("C4").Value
End With

End Sub

For some reason this code filters only one column, while it should be filtering Columns with Number 9 and 23 on 2 different values. As i want to learn from this, explain my thinking error in this piece of VBA. Excel version is 2013, if this makes any difference.

like image 430
RobExcel Avatar asked Sep 11 '25 20:09

RobExcel


1 Answers

Try to seperate the syntax to 2 lines:

Sub FilterOnCellValue()

With Sheets("Dump").Range("A1:Z10000")
    .AutoFilter Field:=9, Criteria1:=Sheets("ControlPlanning").Range("C1").Value
    .AutoFilter Field:=23, Criteria1:=Sheets("ControlPlanning").Range("C4").Value
End With

End Sub
like image 161
Shai Rado Avatar answered Sep 13 '25 12:09

Shai Rado