Dinamic Table Connection VBA

RogerMello

New Member
Joined
May 10, 2017
Messages
1
I'm learning VBA with Bill Jelen book. The code don't work and the problem is about CreateModelConnection. I need to help.

Sub Tabela_Dinamica()

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Dim WBT As Workbook
Dim WC As WorkbookConnection
Dim MO As Model
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim WS As Worksheet
Dim CFRevenue As CubeField
Dim CFCustomer As CubeField

Set WBT = ActiveWorkbook
Set WS = WBT.Worksheets("Resultado")

TableName
= "Vendas"
WBT
.Connections.Add Name:="LinkedTable_" & TableName, Description:="TabelaPrincipal", _
ConnectionString
:="Worksheet;" & WBT.FullName, CommandText:=WBT.Name & "!" & TableName, _
LcMDTYPE
:=7, CreateModelConnection:=True, ImportRelationship:=False

TableName
= "Setor"
WBT
.Connections.Add Name:="LinkedTable_" & TableName, Description:="TabelaPrincipal", _
ConnectionString
:="Worksheet;" & WBT.FullName, CommandText:=WBT.Name & "!" & TableName, _
LcMDTYPE
:=7, CreateModelConnection:=True, ImportRelationship:=False

Set MO = ActiveWorkbook.Model
MO
.ModelRelationships.Add ForeignKeyColumn:=MO.ModelTables("Vendas").ModelTableColumns("Customer"), _
PrimaryKeyColumn
:=MO.ModelTables("Setor").ModelTableColumns("Customer")

For Each PT In WS.PivotTables
PT
.TableRange2.Clear
Next PT

Set PTCache = WBT.PivotCaches.Create(xlExternal, WBT.Connections("ThisWorkbookDataModel"), xlPivotTableVersion15)
Set PT = PTCache.CreatePivotTable(WS.Cells(1, 1), "PivotTable1")

With PT.CubeFields("[Setor].[Sector]")
.Orientation = xlRowField
.Position = 1
End With

Set CFRevenue = PT.CubeFields.GetMeasure(AttributeHierarchy:="[Vendas].[Revenue]", Function:=xlSum, _
Caption
:="Soma da Revenda")
PT
.AddDataField Field:=CFRevenue, Caption:="Total de Revenda"
PT
.PivotFields("Total de Revenda").NumberFormat = "$#,##0,K"

Set CFCustomer = PT.CubeFields.GetMeasure(AttributeHierarchy:="[Vendas].[Customer]", Function:=xlDistinctCount, _
Caption
:="Contagem Clientes")
PT
.AddDataField Field:="Contagem Clientes", Caption:="Contagem Clientes"

End Sub()
</code>
It's on page 264 VBA & Macros Microsoft Excel 2013, Bill Jelen and Tracy Syrstad
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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