Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
#23011
Estou aprendendo VBA por conta própria através da leitura do livro do Bill Jelen. Na parte de conexão com tabelas externas através da tabela dinâmica não consigo rodar o código. Tentei algumas alterações, busquei algumas coisas na internet porém não tive sucesso, gostaria da ajuda de alguém para que o código funcione.

O problema no código encontra-se na parte do CreateModelConnection

Sub Tabela_Dinamica()

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"
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord