Expected End With Compile Error when trying to run VBA for two Pivot tables

MRN227

New Member
Joined
Dec 18, 2018
Messages
15
I'm looking for someone to help me with the below VBA. With one Pivot table on the code the macro runs perfect, but when I add a second table to the code I get a "Expected End With" error:

Sub TestPivot2()
'TestPivot Macro

Dim dt As String
dt = Sheets("Calendar View").Range("valSelEmployee").Value

With ActiveSheet.PivotTables("PivotTable1").PivotFields("Employee Name")
With ActiveSheet.PivotTables("PivotTable2").PivotFields("List of Employees")

.ClearAllFilters
.PivotFilters.Add Type:=xlCaptionEquals, Value1:=dt
End With

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
It seems that you have opened 2 With blocks, but you have closed only one of them.
Just add another End With and it should work.
 
Upvote 0
Also, your statements within both your With statements will only apply to the inner With statement. Instead, try...

Code:
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Employee Name")
    .ClearAllFilters
    .PivotFilters.Add Type:=xlCaptionEquals, Value1:=dt
End With


With ActiveSheet.PivotTables("PivotTable2").PivotFields("List of Employees")
    .ClearAllFilters
    .PivotFilters.Add Type:=xlCaptionEquals, Value1:=dt
End With

Hope this helps!
 
Upvote 0
Hi & welcome to MrExcel.
You need to do them separately like
Code:
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Employee Name")
   .ClearAllFilters
   .PivotFilters.Add Type:=xlCaptionEquals, value1:=dt
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("List of Employees")
   .ClearAllFilters
   .PivotFilters.Add Type:=xlCaptionEquals, value1:=dt
End With
 
Upvote 0
This is my first time posting here and Im totally new to VBA. Thank you all for the help this works great and my HR is very happy. Thank you so much!!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top