1

I need this code to be rewritten to calculate without selecting the "Random_Cal" sheet.

Sheets("Random_Cal").PivotTables("PivotTable3").PivotCache.Refresh Sheets("Random_Cal").Select ActiveSheet.PivotTables("PivotTable3").PivotFields("3a").PivotFilters.Add Type _ :=xlValueIsBetween, DataField:=ActiveSheet.PivotTables("PivotTable3"). _ PivotFields("3b"), Value1:=1, Value2:=Range("AI52") 

The code all works but the button that activates the code is on a different sheet to "Random_Cal". Currently when the button is pressed the screen jumps to "Random_Cal" then jumps back to the starting sheet. I just need this section of code to be altered to stay on the first sheet but to calculate the "Random_Cal" sheet. Thanks

1
  • I have tried this {Sheets("Random_Cal").PivotTables("PivotTable3").PivotCache.Refresh Sheets("Random_Cal").PivotTables("PivotTable3").PivotFields("3a").PivotFilters.Add Type _ :=xlValueIsBetween, DataField:=Sheets("Random_Cal").PivotTables("PivotTable3"). _ PivotFields("3b"), Value1:=1, Value2:=Range("AI52")} But this did not work and i just received the Error 400 code. Commented Jul 30, 2018 at 10:20

1 Answer 1

3

Use a With ... End With block that passes the parent worksheet reference on.

with workSheets("Random_Cal") .PivotTables("PivotTable3").PivotCache.Refresh .PivotTables("PivotTable3").PivotFields("3a").PivotFilters.Add _ Type:=xlValueIsBetween, DataField:=.PivotTables("PivotTable3"). _ PivotFields("3b"), Value1:=1, Value2:=.Range("AI52") end with 

How to avoid using Select in Excel VBA

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.