Dodettesky
New Member
- Joined
- Dec 17, 2015
- Messages
- 4
Hello,
I am fairly new to VBA, having learned only from Bill Jellen's book. I adopted this code from his book, and it works fine if I line it line by line. If I run it by hitting F5, the resulting Pivot table is collapsed. I have to hit the Show/Hide +/- buttons to display the full pivot table. Can you tell me what's wrong, and if possible, how I can improve this code?
Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim pt As PivotTable
Dim pf As PivotField
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Set WSD =Worksheets("OrderDetails")
For Each pt In WSD.PivotTables
pt.TableRange2.Clear
Next pt
FinalRow =WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1,Application.Columns.Count).End(xlToLeft).Column
Set PRange = WSD.Cells(1,1).Resize(FinalRow, FinalCol)
Set PTCache =ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=PRange)
Set pt =PTCache.CreatePivotTable(TableDestination:=WSD.Cells(2, _
FinalCol + 4),TableName:="PivotTable1")
pt.ManualUpdate = True
pt.AddFields RowFields:=Array("PONumber", "Dlv.Date", "Original Cancel Date")
With pt.PivotFields("Total cu.ft.")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
.NumberFormat = "#,##0.0"
.Name = "Sum of Total cu.ft."
End With
pt.ManualUpdate = False
pt.ManualUpdate = True
pt.ShowTableStyleRowStripes = True
pt.TableStyle2 ="PivotStyleMedium2"
With pt
.ColumnGrand = False
.RowGrand = False
.RepeatAllLabels xlRepeatLabels
End With
On Error Resume Next
For Each pf In pt.PivotFields
pf.Subtotals(1) = True
pf.Subtotals(1) = False
Next pf
Range("S2").Select
Thanks in advance for your help!
Dodettesky
I am fairly new to VBA, having learned only from Bill Jellen's book. I adopted this code from his book, and it works fine if I line it line by line. If I run it by hitting F5, the resulting Pivot table is collapsed. I have to hit the Show/Hide +/- buttons to display the full pivot table. Can you tell me what's wrong, and if possible, how I can improve this code?
Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim pt As PivotTable
Dim pf As PivotField
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Set WSD =Worksheets("OrderDetails")
For Each pt In WSD.PivotTables
pt.TableRange2.Clear
Next pt
FinalRow =WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1,Application.Columns.Count).End(xlToLeft).Column
Set PRange = WSD.Cells(1,1).Resize(FinalRow, FinalCol)
Set PTCache =ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=PRange)
Set pt =PTCache.CreatePivotTable(TableDestination:=WSD.Cells(2, _
FinalCol + 4),TableName:="PivotTable1")
pt.ManualUpdate = True
pt.AddFields RowFields:=Array("PONumber", "Dlv.Date", "Original Cancel Date")
With pt.PivotFields("Total cu.ft.")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
.NumberFormat = "#,##0.0"
.Name = "Sum of Total cu.ft."
End With
pt.ManualUpdate = False
pt.ManualUpdate = True
pt.ShowTableStyleRowStripes = True
pt.TableStyle2 ="PivotStyleMedium2"
With pt
.ColumnGrand = False
.RowGrand = False
.RepeatAllLabels xlRepeatLabels
End With
On Error Resume Next
For Each pf In pt.PivotFields
pf.Subtotals(1) = True
pf.Subtotals(1) = False
Next pf
Range("S2").Select
Thanks in advance for your help!
Dodettesky