Macro that won't work on all sheets

montanaheather

New Member
Joined
Oct 31, 2011
Messages
25
Hello,

I am trying to write a macro for a pivot table that will work on all sheets in my workbook. Each sheet represents a different month, and the pivot table is meant to summarize budget information for each month. Below is the code. Please help!

Sub BudgetMacro1()
'
' BudgetMacro1 Macro
'
'
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R35C5", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Sheet1!R4C9", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12
Sheets("Sheet1").Select
Cells(4, 9).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Date")
.Orientation = xlRowField
.Position = 1
End With
End Sub
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Yeah, but I am not sure how to replace it, I think. How do I handle the "" marks around Sheet1? See below.

Sub BudgetMacro1()
'
' BudgetMacro1 Macro
'
'
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"activesheet!R1C1:R35C5", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="activesheet!R4C9", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12
Sheets("activesheet").Select
Cells(4, 9).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Date")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Category")
.Orientation = xlColumnField
.Position = 1
End With
 
Upvote 0
Sorry, this is not as easy as it looks, I tried recording a script and it will not playback, I searched the internet for a while and there were no solutions that I could find.

I hope that someone else can help you.
 
Upvote 0
Code:
Sub BudgetMacro1()
    '
    ' BudgetMacro1 Macro
    '
    '
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=[COLOR="Red"]Range("A1:E35")[/COLOR], _
                                      Version:=xlPivotTableVersion12).CreatePivotTable _
                                      TableDestination:=[COLOR="Red"]Range("I4")[/COLOR], TableName:="PivotTable1", _
                                      DefaultVersion:=xlPivotTableVersion12
    
    Cells(4, 9).Select
    ActiveWorkbook.ShowPivotTableFieldList = True
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Date")
        .Orientation = xlRowField
        .Position = 1
    End With
End Sub

No need to reselect the Active sheet.
 
Upvote 0

Forum statistics

Threads
1,222,248
Messages
6,164,814
Members
451,918
Latest member
Pinnacleinfotechcad

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