Excel VBA为Power Pivot表创建数据模型连接以获取不同的值-错误
在宏中,我试图根据两个条件获得不同的值进行计数。以前,我在外部报告中使用了一个公式来添加计数=IFERROR(行)(唯一(筛选器)()这对大多数供应商都有效,但是当它计数的原始数据在其他供应商上变得太大时,它会出错,并且只显示1或0 我已切换到尝试将原始数据表添加到数据透视表中,并将其添加到与原始数据表相同工作簿中的数据模型中,这样我就可以轻松获得一个不同的计数列,然后使用GETPIVOTDATA将计数添加到外部报表中的行中。这都是较大宏的一部分。这在某些供应商身上效果良好,创建数据模型连接时出现其他错误。“运行时错误1004:数据模型出现问题,导致Microsoft Excel无法打开此工作簿。请尝试重新启动Microsoft Excel。” 在创建新连接和新数据透视表之前,我已尝试清除连接,但仍然出现错误。当我停止宏以测试添加power pivot表并手动添加到模型时,我的所有PowerPivot选项以及在尝试通过普通数据透视选项卡添加数据模型时将其添加到数据模型的选项都变灰选择权 这是我第一次在宏中使用PowerPivot,但在使用大数据集时,如果不挂起数据集或只给出“1”或“0”,我似乎找不到更容易获得不同值的解决方案 当前代码:Excel VBA为Power Pivot表创建数据模型连接以获取不同的值-错误,excel,vba,powerpivot,datamodel,distinct-values,Excel,Vba,Powerpivot,Datamodel,Distinct Values,在宏中,我试图根据两个条件获得不同的值进行计数。以前,我在外部报告中使用了一个公式来添加计数=IFERROR(行)(唯一(筛选器)()这对大多数供应商都有效,但是当它计数的原始数据在其他供应商上变得太大时,它会出错,并且只显示1或0 我已切换到尝试将原始数据表添加到数据透视表中,并将其添加到与原始数据表相同工作簿中的数据模型中,这样我就可以轻松获得一个不同的计数列,然后使用GETPIVOTDATA将计数添加到外部报表中的行中。这都是较大宏的一部分。这在某些供应商身上效果良好,创建数据模型连接时出
Dim wsPivotModel As Worksheet
Dim SDCPivotCache As PivotCache
Dim PvtSDCmodel As PivotTable
Dim SDCconnection As WorkbookConnection
Set wsPivotModel = MainWB.Sheets.Add
' delete existing internal connections if necessary
For Each SDCconnection In MainWB.Connections
If SDCconnection.Type = xlConnectionTypeWORKSHEET Then SDCconnection.Delete
Next SDCconnection
'Create new connection
Set SDCconnection = MainWB.Connections.Add2("WorksheetConnection_" & MainWB.Name & "!Table_SDCdata", "", _
"WORKSHEET;" & DataPath & "\" & MainWB.Name, MainWB.Name & "!Table_SDCdata", 7, True, False)
'create and configure new pivotcache
Set SDCPivotCache = MainWB.PivotCaches.Create(SourceType:=xlExternal, SourceData:=SDCconnection, Version:=6)
With SDCPivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsNone
End With
'Create and configure new pivottable
Set PvtSDCmodel = SDCPivotCache.CreatePivotTable(TableDestination:=wsPivotModel.Name & "!R1C1", TableName:="PvtSDCmodel", DefaultVersion:=6)
With wsPivotModel.PivotTables("PvtSDCmodel")
.RepeatAllLabels xlRepeatLabels
With .CubeFields("[Table_SDCdata].[Region]")
.Orientation = xlRowField
.Position = 1
End With
With .CubeFields("[Table_SDCdata].[Format]")
.Orientation = xlRowField
.Position = 2
End With
.CubeFields.GetMeasure "[Table_SDCdata].[Site]", xlCount, "Count of Site"
.AddDataField .CubeFields("[Measures].[Count of Site]"), "Count of Site"
.InGridDropZones = True
.RowAxisLayout xlTabularRow
With .PivotFields("[Measures].[Count of Site]")
.Caption = "Distinct Count of Site"
.Function = xlDistinctCount
End With
End With
'Get store format count in province from SDC
Dim ref As Variant
Dim sFormula As String
ref = "[" & MainWB.Name & "]" & wsPivotModel.Name
sFormula = "=GETPIVOTDATA(""[Measures].[Distinct Count of Site]"",'" & ref & "'!$A$1,""[Table_SDCdata].[Region]"",""[Table_SDCdata].[Region].&[""&[@Region]&""]"",""[Table_SDCdata].[Format]"",""[Table_SDCdata].[Format].&[""&[@Format]&""]"")"
With wb.Worksheets("Number of Stores Ranged").ListObjects("Table_Number_of_Stores_Ranged")
With .ListColumns("Total Number of Stores : In Province").DataBodyRange
.NumberFormat = "General"
.Formula = sFormula
.Value = .Value
End With
有没有关于为什么会发生这个错误并在事后将所有选项变灰的建议?
或者如果Power Pivot不起作用(这是应该的),那么关于如何从大量数据中轻松获取不同计数而不失败的任何其他建议呢?您是否已在PP中创建了作为DISTINCTCOUNT的度量值?如果是,请查看Excel函数CUBEMEMBER()和CUBEVALUE()。使用此函数,您不需要数据透视表,您可以直接访问数据模型。谢谢,@Chris,我将对此进行研究,但首先我需要解决为数据模型创建连接的问题,这是我遇到的主要问题。