Why pivot table is collapsed if not run line by line

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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,215,972
Messages
6,128,037
Members
449,414
Latest member
sameri

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