Type mismatch error when creating a pivot table in Excel with VBA

后端 未结 1 1818
南旧
南旧 2020-12-21 02:10

I\'m trying to put a macro together that will make a simple pivot table using the data from an active worksheet. When I try to run it, I receive a type mismatch error. Whe

1条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 02:26

    This worked for me (XL2007):

    Sub Tester()
    
        With ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
            SourceData:=ActiveSheet.UsedRange)
    
            .CreatePivotTable TableDestination:="Sheet1!R3C1", _
            TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
    
        End With
    
        With Sheet1.PivotTables("PivotTable1")
            .PivotFields("Dept Head").Orientation = xlColumnField
            .PivotFields("Program Name").Orientation = xlRowField
            .AddDataField .PivotFields("Cost"), "Sum of cost", xlSum
        End With
    
    End Sub
    

    Make sure you don't already have an existing conflicting pivot cache/table.

    0 讨论(0)
提交回复
热议问题