使用 VBA 获取运行时错误的数据透视缓存

pivot cache using vba getting run time error

提问人:Govinda Rathi 提问时间:10/7/2023 最后编辑:JohnMGovinda Rathi 更新时间:10/7/2023 访问量:27

问:

我创建了一个脚本,该脚本在我的笔记本电脑上运行良好,但是当我的同事尝试使用相同的脚本时,透视缓存出现错误

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=1, SourceData:=range("A1:AV" & LastRow))
Sub pivot_qty()
    
    Dim PCache As PivotCache, LastRow As Long, pt As PivotTable
    
    
      'If "Pivot" worksheet already exists, delete it
    
    
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Pivot qty").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    
     LastRow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
       
    
     Worksheets("Sheet1").Activate
     Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=1, SourceData:=range("A1:AV" & LastRow))
         Worksheets.Add
        ActiveSheet.Name = "Pivot qty"
     Set pt = ActiveSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=range("A1"), TableName:="PivotTable1")
     
     'Select fields for PivotTable
         ActiveWorkbook.ShowPivotTableFieldList = True
      
        With pt.PivotFields("Plant")
            .Orientation = xlRowField
            .Position = 1
        End With
        
         With pt.PivotFields("Service Desc")
            .Orientation = xlRowField
            .Position = 2
        End With
        
        
         With pt.PivotFields("Price Per Unit")
            .Orientation = xlRowField
            .Position = 3
        End With
         
     ActiveWorkbook.ShowPivotTableFieldList = False
         
End Sub
VBA 数据透视表 Excel-2016

评论

0赞 JohnM 10/7/2023
一个猜测是,在那行上使用不合格。但是,打电话给您确认错误号和确切的错误文本?Range
0赞 Govinda Rathi 10/9/2023
@JohnM我收到错误 438 ,对象不支持此属性
0赞 JohnM 10/9/2023
请参阅 PivotCaches.Create 的 MS 文档,其中说“传递 Range 对象时,我们建议您使用字符串来指定工作簿、工作表和单元格范围,或者设置命名范围并将名称作为字符串传递。传递 Range 对象可能会导致意外的“类型不匹配”错误”

答: 暂无答案