我使用宏录制器创建此代码以自动获取Pivot表。
但是当我再次运行此代码时会出现一条错误消息:
运行时错误1004:无效的引用
此行Workbooks("works.xlsm").Connections.Add2
。
如果录制了此代码,为什么会出现无效引用?在录制过程中,我为表格提供了名称“数据库”(R1C4:R18532C9)。我使用Windows 10和Office 2016。
Range("D1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="database", RefersToR1C1:= _
"=Data!R1C4:R18532C9"
ActiveWorkbook.Names("database").Comment = ""
Range("D1").Select
Workbooks("works.xlsm").Connections.Add2 _
"WorksheetConnection_works.xlsm!database", "", _
"WORKSHEET;C:\Users\gabor\Documents\CAFM\VBS\works.xlsm", _
"works.xlsm!database", 7, True, False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("WorksheetConnection_works.xlsm!database"), _
Version:=6).CreatePivotTable TableDestination:="Pivot!R1C1", TableName:= _
"Statement1", DefaultVersion:=6
Sheets("Pivot").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("Statement1").CubeFields("[database].[Person]")
.Orientation = xlRowField
.Position = 1
End With
The database table and the pivot results with xlCount and xlDistinctCount
答案 0 :(得分:1)
尝试下面的编辑代码,解释在代码中作为注释。
xlDistinctCount
未经测试,因为我有Office 2010(可从Officde 2013获得),但应该可以使用。
<强>代码强>
Option Explicit
Sub AutoDynamicPivot()
Dim PT As PivotTable
Dim PTCache As PivotCache
Dim WB As Workbook
Dim Sht As Worksheet
Dim SrcData As Variant
Dim lRow As Long, lCol As Long
Set WB = ThisWorkbook
Set Sht = WB.Worksheets("Data") '<-- set the "Data" worksheet
lRow = Sht.Range("A1").End(xlDown).Row '<-- modifed from "D1" to "A1" (according to PO screen-shot)
lCol = Sht.Range("A1").End(xlToRight).Column '<-- modifed from "D1" to "A1" (according to PO screen-shot)
' set the Named Range "database" to the data in worksheet "Data"
WB.Names.Add Name:="database", RefersToR1C1:="=" & Sht.Name & "!R1C1:R" & lRow & "C" & lCol '<-- modifed to "R1C1" (according to PO screen-shot)
WB.Names("database").Comment = ""
' Determine the data range you want for your Pivot Cache
Set SrcData = Range("database")
' set the Pivot Cache
Set PTCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, SrcData)
' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PT = Worksheets("Pivot").PivotTables("Statement1") ' check if "Statement1" Pivot Table already created (in past runs of this Macro)
On Error GoTo 0
If PT Is Nothing Then
' create a new Pivot Table in "Pivot" sheet, start from Cell A1
Set PT = Worksheets("Pivot").PivotTables.Add(PivotCache:=PTCache, TableDestination:=Worksheets("Pivot").Range("A1"), TableName:="Statement1")
'Create the headings and row and column orientation and all of your other settings here
With PT
' set "Person" as rows field
With .PivotFields("Person")
.Orientation = xlRowField
.Position = 1
End With
' set "Month" as Filter
With .PivotFields("Month")
.Orientation = xlPageField
.Position = 1
End With
' set "Count of Cases"
.AddDataField .PivotFields("Case"), "Count of Case", xlCount
' set "Distinct Count of Cases"
.AddDataField .PivotFields("Case"), "Distinct Count of Case", xlDistinctCount
End With
Else
' just refresh the Pivot cache with the updated Range (data in "Data" worksheet)
PT.ChangePivotCache PTCache
PT.RefreshTable
End If
End Sub
使用此代码创建的数据透视表的屏幕截图:
答案 1 :(得分:0)
我在&#34; works.xlsm!database&#34;中更改了Connections.Add2中的commandtext参数。到&#34;数据!数据库&#34;。它解决了这个问题。我也编辑了ActiveWorkbook.Names.Add。
LastRow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Sheets("Data").Cells(1, Columns.Count).End(xlToLeft).Column
ActiveWorkbook.Names.Add _
Name:="database", _
RefersTo:="=Data!R1C4:R" & LastRow & "C" & LastCol & ""
ActiveWorkbook.Connections.Add2 _
"WorksheetConnection_works.xlsm!database", "", _
"WORKSHEET;C:\Users\gabor\Documents\CAFM\VBS\works.xlsm", _
"Data!database", 7, True, False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("WorksheetConnection_works.xlsm!database"), _
Version:=6).CreatePivotTable TableDestination:="Pivot!R1C1", TableName:= _
"Statement1", DefaultVersion:=6
Sheets("Pivot").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("Statement1").CubeFields("[database].[Person]")
.Orientation = xlRowField
.Position = 1
End With
我可以使用以下代码获取xlDistinctCount:
ActiveSheet.PivotTables("Statement1").CubeFields.GetMeasure "[database].[TT]" _
, xlCount
ActiveSheet.PivotTables("Statement1").AddDataField ActiveSheet.PivotTables( _
"Statement1").CubeFields("[Measures].[quantity of items - TT]")
With ActiveSheet.PivotTables("Statement1").PivotFields( _
"[Measures].[quantity of items - TT]")
.Caption = "number of distinct items – TT"
.Function = xlDistinctCount
End With
我必须先使用xlCount,这个结果可以得到xlDistinctCount。