Hi,
I'm trying to create multiple pivot tables in a worksheet, both of which use data models. The code is as below
Sub CreateMIR()
Set ResultBk = Workbooks("Pending PR - MIR - 09.03.2016.xlsx")
Set TotalPrSht = ResultBk.Worksheets("Total PR Details")
Set LatePrSht = ResultBk.Worksheets(">75 Days Details")
Set PivotSht = ResultBk.Worksheets.Add(LatePrSht)
With PivotSht
.Name = "Summary"
For Each ResultSht In Array(LatePrSht, TotalPrSht)
ResultBk.Connections.Add2 "Connection" & ResultSht.Index, "", "WORKSHEET;" & ResultBk.Path & "\[" & ResultBk.Name & "]" & _
ResultSht.Name, ResultSht.Name & "!R1C1:R" & ResultSht.UsedRange.Rows.Count & "C12", 7, True, False
ResultBk.PivotCaches.Create(xlExternal, ResultBk.Connections("Connection" & ResultSht.Index), xlPivotTableVersion15).CreatePivotTable _
"Summary!R3C" & (((ResultSht.Index - 2) * 6) + 1), "Pivot" & ResultSht.Index, DefaultVersion:=xlPivotTableVersion15
With .PivotTables("Pivot" & ResultSht.Index)
.CubeFields("[Range].[Purch. Area]").Orientation = xlRowField
.CubeFields("[Range].[PGr]").Orientation = xlRowField
.CubeFields.GetMeasure "[Range].[Material]", xlCount, "Count of Material"
.AddDataField .CubeFields("[Measures].[Count of Material]"), "No. of Line Items"
.CubeFields.GetMeasure "[Range].[Material]", xlDistinctCount, "Distinct Count of Material"
.AddDataField .CubeFields("[Measures].[Distinct Count of Material]"), "No. of Matl codes"
.CubeFields.GetMeasure "[Range].[PR Value]", xlSum, "Sum of PR Value"
.AddDataField .CubeFields("[Measures].[Sum of PR Value]"), "Total PR Value"
.PivotFields("[Measures].[Sum of PR Value]").NumberFormat = "#,##0"
.PivotFields("[Range].[PGr].[PGr]").AutoSort xlDescending, "[Measures].[Sum of PR Value]", .PivotColumnAxis.PivotLines(3), 1
.CubeFields("[Range].[Remarks]").Orientation = xlPageField
.PivotFields("[Range].[Remarks].[Remarks]").CurrentPageName = "[Range].[Remarks].&"
.TableStyle2 = "PivotStyleMedium8"
End With
Next
End With
End Sub
Please refer the attachment given below. Both pivot tables gets created fine. But, the second pivot table does not use the new connection reference and uses the data of the old file itself. Any idea y? I'm using , Excel 2013, 64 bit version.
Bookmarks