'Why does my VBA code throw the Runtime Error 1004 when I am trying to have a button on another worksheet run this macro? Below is the code processed
Below is the code I want to run. I want to be able to click a button on one page and have the other page run this on the pivot table. I will also include the code for the button, which actually my be what is causing the issue.
Private Sub CommandButton1_Click()
Call OHPivot_Store80
End Sub
Sub OHPivot_Store80()
'
' OHPivot_Store80 Macro
'
'
ActiveSheet.PivotTables("PivotTable1").PivotFields("StoreNumber").CurrentPage _
= "(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("StoreNumber")
.PivotItems("0").Visible = False
.PivotItems("6").Visible = False
.PivotItems("22").Visible = False
.PivotItems("24").Visible = False
.PivotItems("40").Visible = False
.PivotItems("44").Visible = False
.PivotItems("48").Visible = False
.PivotItems("50").Visible = False
.PivotItems("54").Visible = False
.PivotItems("56").Visible = False
.PivotItems("58").Visible = False
.PivotItems("60").Visible = False
.PivotItems("62").Visible = False
.PivotItems("64").Visible = False
.PivotItems("70").Visible = False
.PivotItems("72").Visible = False
.PivotItems("74").Visible = False
.PivotItems("76").Visible = False
.PivotItems("78").Visible = False
.PivotItems("82").Visible = False
.PivotItems("84").Visible = False
.PivotItems("86").Visible = False
.PivotItems("88").Visible = False
.PivotItems("90").Visible = False
.PivotItems("92").Visible = False
.PivotItems("94").Visible = False
.PivotItems("96").Visible = False
.PivotItems("98").Visible = False
.PivotItems("110").Visible = False
.PivotItems("(blank)").Visible = False
End With
End Sub
Solution 1:[1]
Change the code that responds to the button click to active the sheet that has the pivot table as follows:
Private Sub CommandButton1_Click()
Worksheets.Activate("NAME_OF_SHEET_WITH_PIVOT_TABLE")
Call OHPivot_Store80
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gove |
