Pivot Table Data Model referencing wrong range

Andy2021

New Member
Joined
Mar 4, 2021
Messages
11
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have created some code that created a data model based pivot table that just displays a type, qty and concatenated string built from a column of references. This all works fine on the first run. Where it goes wrong is if I run it again on the same worksheet, even defining a different range, it is pulling in the total of the qty from the first table.
When I look at the Pivot Table field list it has actually ticked the Qty field from the first table, and not the same field from the second. As far as I can see I have made the code that it uniquely identifies the table it should be using.
Any help would be gratefully received.
Here are the parts I believe to be relevant.

VBA Code:
    'Define Source Range and location of Pivot Table
    Set Source_Range = Application.InputBox("Select Source Table:", "WT_PivotTable", Selection.Address(0, 0), Type:=8)
    Set PT_Location = Application.InputBox("Select PivotTable Location:", "WT_PivotTable", Selection.Address(0, 0), Type:=8)
    PT_Name = "WT_PT_" & CountPivotsInWorkbook
    PT_Conn = "PT_Connection" & CountPivotsInWorkbook
    Range_Name = "Range_" & CountPivotsInWorkbook
    ActiveWorkbook.ActiveSheet.Range(Source_Range.Address).Name = Range_Name
    myDestinationRange = ActiveSheet.Name & "!" & PT_Location.Address(ReferenceStyle:=xlR1C1)
    

    'Connection
    ActiveWorkbook.Connections.Add2 _
    Name:=PT_Conn, _
    Description:="", _
    ConnectionString:="WORKSHEET;" & ActiveWorkbook.FullName & "]" & ActiveSheet.Name, _
    CommandText:=ActiveWorkbook.Name & "!" & Range_Name, _
    lCmdtype:=7, _
    CreateModelConnection:=True, _
    ImportRelationships:=False

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
    ActiveWorkbook.Connections(PT_Conn), Version _
    :=7).CreatePivotTable TableDestination:=myDestinationRange, _
    TableName:=PT_Name, DefaultVersion:=7


            With Source_Range
               Col1_Title = ActiveSheet.Range(.Cells(1).Address(0, 0)).Value        'Type
               Col2_Title = ActiveSheet.Range(.Cells(1, 2).Address(0, 0)).Value     ' Ref
               Col3_Title = ActiveSheet.Range(.Cells(1, 3).Address(0, 0)).Value     ' Qty
            End With

            With ActiveSheet.PivotTables(PT_Name).CubeFields("[" & Range_Name & "].[" & Col1_Title & "]")
                .Orientation = xlRowField
                .Position = 1
            End With
            ActiveSheet.PivotTables(PT_Name).CubeFields.GetMeasure "[" & Range_Name & "].[" & Col3_Title & "]", xlSum, "Sum of Qty" & CountPivotsInWorkbook
            ActiveSheet.PivotTables(PT_Name).AddDataField ActiveSheet.PivotTables(PT_Name).CubeFields("[Measures].[Sum of Qty]"), "Sum of Qty" & CountPivotsInWorkbook
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Shouldn't you be using:

Code:
CubeFields("[Measures].[Sum of Qty" & CountPivotsInWorkbook & "]")
in the last line?
 
Upvote 0
Shouldn't you be using:

Code:
CubeFields("[Measures].[Sum of Qty" & CountPivotsInWorkbook & "]")
in the last line?

Thank you, but trying this throws up a subscript out of range error.
 
Upvote 0
Then it would appear that your previous line either didn't work, or gave the measure a different name. I suggest you check manually what the measure is called now.
 
Upvote 0
A bit of additional info.
When I run my code and look at the field list for the second pivot table, you can see here that it has somehow referenced the field in the first range.
If I just click on the correct one in Range_1 it's all fine.

1634033550206.png
 
Upvote 0
That's because you're still using [Sum of Qty] as the measure name. You might be better off using a CubeField object like this:

Code:
            Set SumField = ActiveSheet.PivotTables(PT_Name).CubeFields.GetMeasure("[" & Range_Name & "].[" & Col3_Title & "]", xlSum, "Sum of Qty" & CountPivotsInWorkbook)
            ActiveSheet.PivotTables(PT_Name).AddDataField SumField, "Sum of Qty" & CountPivotsInWorkbook
 
Upvote 0
Solution
That's because you're still using [Sum of Qty] as the measure name. You might be better off using a CubeField object like this:

Code:
            Set SumField = ActiveSheet.PivotTables(PT_Name).CubeFields.GetMeasure("[" & Range_Name & "].[" & Col3_Title & "]", xlSum, "Sum of Qty" & CountPivotsInWorkbook)
            ActiveSheet.PivotTables(PT_Name).AddDataField SumField, "Sum of Qty" & CountPivotsInWorkbook
Fantastic thank you so much.
You stare at code for so long you go blind to it.

I really appreciate your fast response and it worked beautifully!

This was my first time creating pivot tables with data models using VBA so I was happy I got this far.
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,400
Members
449,448
Latest member
Andrew Slatter

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