Excel Pivot. need help on Pivot Measure and the table name

bobbyexcel

Board Regular
Joined
Nov 21, 2019
Messages
88
Office Version
  1. 365
Platform
  1. Windows
I need help in creating a pivot table with Measure which is a part of my complete code. The issue is, whenever I create measure, it creates a model table with a unique name everytime. so my code is unable to read the variable (table name) to fetch data from the columns. Is there a way that I can define the table name and pass it as an argument while creating Pivot ?

Here is my code..


Dim chk As String
Dim mdlt As ModelTable
Dim strName As String

Set mdlt = ActiveWorkbook.Model.ModelTables(Index:=1)
chk = ActiveWorkbook.Model.ModelTables(Index:=1)
strName = ActiveWorkbook.Model.ModelTables(1).Name



Set PSheet = Worksheets("PivotTable_temp")
Set DSheet = Worksheets("Final_bkp")

lastRowNew = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastColNew = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(lastRowNew, LastColNew)

'Define Pivot Cache

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:=ActiveWorkbook.Connections("WorksheetConnection_Final_bkp!$A:$H1"), Version:=7).CreatePivotTable(TableDestination:=PSheet.Cells(3, 1), tableName:="MySTable", DefaultVersion:=7)


'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(3, 1), tableName:="MySTable")
ActiveSheet.PivotTables("MySTable").PivotCache.RefreshOnFileOpen = False
ActiveSheet.PivotTables("MySTable").RepeatAllLabels xlRepeatLabels

With ActiveSheet.PivotTables("MySTable").CubeFields("[Range 2].[Server Name]")
.Orientation = xlRowField
.Position = 1
End With


I tried to pass the above with statment with the "strName" name as the table name but didnt work since the table name which is active is different than the output of the this (strName = ActiveWorkbook.Model.ModelTables(1).Name).. here is the with statement I tried

With ActiveSheet.PivotTables("MySTable").CubeFields("["strName"].[Server Name]")
.Orientation = xlRowField
.Position = 1
End With


Appreciate if anyone helps me on this..

This entire code is to keep a percentage column in pivot table ...
 

Attachments

  • tempcap.PNG
    tempcap.PNG
    6 KB · Views: 7
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I need help in creating a pivot table with Measure which is a part of my complete code. The issue is, whenever I create measure, it creates a model table with a unique name everytime. so my code is unable to read the variable (table name) to fetch data from the columns. Is there a way that I can define the table name and pass it as an argument while creating Pivot ?

Here is my code..


Dim chk As String
Dim mdlt As ModelTable
Dim strName As String

Set mdlt = ActiveWorkbook.Model.ModelTables(Index:=1)
chk = ActiveWorkbook.Model.ModelTables(Index:=1)
strName = ActiveWorkbook.Model.ModelTables(1).Name



Set PSheet = Worksheets("PivotTable_temp")
Set DSheet = Worksheets("Final_bkp")

lastRowNew = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastColNew = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(lastRowNew, LastColNew)

'Define Pivot Cache

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:=ActiveWorkbook.Connections("WorksheetConnection_Final_bkp!$A:$H1"), Version:=7).CreatePivotTable(TableDestination:=PSheet.Cells(3, 1), tableName:="MySTable", DefaultVersion:=7)


'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(3, 1), tableName:="MySTable")
ActiveSheet.PivotTables("MySTable").PivotCache.RefreshOnFileOpen = False
ActiveSheet.PivotTables("MySTable").RepeatAllLabels xlRepeatLabels

With ActiveSheet.PivotTables("MySTable").CubeFields("[Range 2].[Server Name]")
.Orientation = xlRowField
.Position = 1
End With


I tried to pass the above with statment with the "strName" name as the table name but didnt work since the table name which is active is different than the output of the this (strName = ActiveWorkbook.Model.ModelTables(1).Name).. here is the with statement I tried

With ActiveSheet.PivotTables("MySTable").CubeFields("["strName"].[Server Name]")
.Orientation = xlRowField
.Position = 1
End With


Appreciate if anyone helps me on this..

This entire code is to keep a percentage column in pivot table ...
Or if there is no answer, then I would like to duplicate the existing column and do the calculation without using measure but using the below function.. but I need help in this function.

```````
With ActiveSheet.PivotTables("PivotTableName").PivotFields("ColumnName")
.Orientation = xlDataField
.Function = xlSum
.Calculation = ["Total Success"] / ["Total Jobs"]
.NumberFormat = "0.00%;-"
End With
```````
 
Upvote 0

Forum statistics

Threads
1,215,231
Messages
6,123,756
Members
449,120
Latest member
Aa2

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