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
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