Excel VBA为Power Pivot表创建数据模型连接以获取不同的值-错误

问题描述

在宏中,我试图基于2个标准来获取不同的值进行计数。以前,我曾在外部报告中使用公式添加计数 = IFERROR(ROWS(UNIQUE(FILTER(。)。这适用于大多数供应商,但是当从中计算原始数据时,对其他供应商来说,它很大,它只会显示1或0。

我已切换为尝试将原始数据表添加到数据透视表中,并将其添加到与原始数据表相同的工作簿中的数据模型中,以便我可以轻松地获得不同的count列,然后使用 GETPIVOTDATA 即可将计数添加到外部报告中的行。这都是较大宏的一部分。在某些供应商上这可以正常工作,而其他供应商在创建数据模型连接时会出错。 “运行时错误1004:数据模型存在问题,导致Microsoft Excel无法打开此工作簿。请尝试重新启动Microsoft Excel。”

在创建新的连接和新的数据透视表之前,我尝试清除连接,但是仍然出现错误。当我停止宏以测试添加超级数据透视表并手动添加到模型时,我的所有PowerPivot选项以及尝试通过普通“数据透视表”选项添加到数据模型的选项均显示为灰色。

这是我第一次在宏中使用PowerPivot,但是在使用大数据集而不挂起或仅给出“ 1”或“ 0”的情况下,我似乎找不到更简单的解决方案来获得不同的值。

当前代码

    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无法正常工作的情况下轻松地从大量数据中获得不重复计数(应该这样做)?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)