2017-04-20 106 views
0

我记录了一个生成非常简单的数据透视表的宏。当我播放宏时,我在PivotTable中遇到错误。带有宏的Excel数据透视表

我得到:

无效的过程调用或参数

等我回去,把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 
+0

您是否想每次创建一个新的数据透视表?因为您可以创建一个只更新工作簿中当前包含的数据透视表的宏。使用此当前设置,您将只使用来自已定义参数的数据。所以它只会根据来自A1:A65536的输入创建一个数据透视表,所以如果你下一次说66K行,宏将只基于原来定义的〜65K。 – BWMustang13

+0

是的。这是每个月的新报告。所以我们每次都会在新文档上运行宏。 –

回答

2

你想从工作表中删除数据透视表。在运行宏之前。

1

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

之后,它将在“GF Response Detail R”表单上创建一个新的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 
+0

我得到的设置PivTbl = –

+0

“无效的过程调用或参数” @MattWiner尝试编辑的代码(改变了'PivCache'线) –

+0

我尝试了全新的丢球只有在它的列的数据。在PivTble线上仍然收到'无效的程序' –

0

这里试试这个。这是@Shai Rado建议的细微变化。

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 
+0

一旦你定义了'set PTable'并有'有了PTable',你不需要'随着ActiveSheet.PivotTables( “RegionCountTable”)。透视字段( “区域”)'只是使用'.PivotFields (“地区”)' –

+0

谢谢大家的帮助。很遗憾没有取得任何进展。不知道它是我的Excel版本还是我做的完全错误的。我仍然得到'无效的过程调用或参数' –

+0

我得到它运行到目前为止的唯一方法是通过添加一个新工作表(我会确定),将其设置为活动工作表。然后设置'TableDestination:=“”',但结果只是总数'“Count of Region”“148”' –