带有macros的Excel数据透视表

我logging了一个生成一个非常简单的数据透视表的macros。 当我播放macros,我在PivotTable出现错误。

我得到:

无效的过程调用或参数

所以我回去把单引号放在SourceDataTableDestination周围。 现在我得到一个数据透视表,但只有总数。 它应该给我A列中所有项目的出现次数。

这是代码

  Sub testpivot() ' ' testpivot Macro ' ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "'GF Response Detail R'!R1C1:R65536C1", Version:= _ xlPivotTableVersion10).CreatePivotTable TableDestination:= _ "'GF Response Detail R'!R2C10", TableName:="PivotTable1", _ DefaultVersion:=xlPivotTableVersion10 Sheets("GF Response Detail R").Select Cells(2, 7).Select With ActiveSheet.PivotTables("PivotTable1").PivotFields("Region") .Orientation = xlRowField .Position = 1 End With ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _ "PivotTable1").PivotFields("Region"), "Count of Region", xlCount ActiveWorkbook.ShowPivotTableFieldList = False End Sub 

您想要从工作表中删除数据透视表。 在运行macros之前。

下面的代码将首先检查工作表中是否存在“PivotTable1”,如果有,则会将其删除。

之后,它将在“GF Response Detail R”表单上创build一个新的PivotTable表,并在列“A”中更新数据。

 Option Explicit Sub testpivot() ' testpivot Macro Dim PivTbl As PivotTable Dim PivCache As PivotCache Dim DataSht As Worksheet Dim lastRow As Long Dim SrcRng As Range Dim SrcData As String ' set the Pivot Data Set DataSht = Worksheets("GF Response Detail R") With DataSht lastRow = .Range("A" & .Rows.Count).End(xlUp).Row '<-- get last row in Column A Set SrcRng = .Range("A1:A" & lastRow) '<-- set dynamic Pivot Range SrcData = SrcRng.Address(True, True, xlA1, xlExternal) '<-- get the Range Address, including sheet's name End With '-- first check if there's a "PivotTable1" in sheet >> if Yes, Delete it For Each PivTbl In DataSht.PivotTables If PivTbl.Name = "PivotTable1" Then DataSht.Range(PivTbl.TableRange2.Address).Delete Shift:=xlUp Exit For End If Next PivTbl ' set the Pivot Cache 'Set PivCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData) ' Option 2: set the Pivot Cache Set PivCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcRng) ' Option 3: set the Pivot Cache Set PivCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData, Version:=xlPivotTableVersion15) ' create a new Pivot Table in "EXP Pivot" sheet, start from Cell A1 Set PivTbl = DataSht.PivotTables.Add(PivotCache:=PivCache, TableDestination:=DataSht.Range("J2"), TableName:="PivotTable1") With PivTbl With .PivotFields("Region") .Orientation = xlRowField .Position = 1 End With .AddDataField .PivotFields("Region"), "Count of Region", xlCount ActiveWorkbook.ShowPivotTableFieldList = False End With End Sub 

这里试试这个。 这是@Shai Radobuild议的一个细微变化。

 Sub RegionMacro() Dim DSheet As Worksheet Dim PCache As PivotCache Dim PTable As PivotTable Dim PRange As Range Dim LastRow As Long 'define the sheet, last row, and pivot table range Set DSheet = Worksheets("GF Response Detail R") LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row Set PRange = DSheet.Range("A1:A" & LastRow) 'get address of range sData = PRange.Address(False, True) 'check to see if the table already exist and delete it if it does For Each PTable In DSheet.PivotTables If PTable.Name = "RegionCountTable" Then DSheet.Range(PTable.TableRange2.Address).Delete Shift:=xlUp Exit For End If Next PTable 'define the pivot cache Set PCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) 'insert a blank pivot table into cell G2 and call it "RegionCountTable" Set PTable = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(2, 7), TableName:="RegionCountTable") 'insert row fields With PTable With ActiveSheet.PivotTables("RegionCountTable").PivotFields("Region") .Orientation = xlRowField .Position = 1 End With 'create a count for the region .AddDataField .PivotFields("Region"), "Count of Region", xlCount ActiveWorkbook.ShowPivotTableFieldList = False End With End Sub