我正在尝试将一个数据透视表添加到我所在的同一工作表中(该工作表称为holder(corp))但是我遇到了麻烦。
Sub PivotTable()
Sheets("Sheet2").Select
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim pf As PivotField
Dim StartPvt As String
Dim SrcData As String
Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).row
'Determine the data range you want to pivot
SrcData = ActiveSheet.Name & "!" & Range(Cells(1, "A"), Cells(LastRow,"E")).Address(ReferenceStyle:=xlR1C1)
'Create a new worksheet
Set sht = Sheets("HOLDERS (CORP)")
'Where do you want Pivot Table to start?
StartPvt = sht.Name & "!" & sht.Range("A1").Address(ReferenceStyle:=xlR1C1)
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)
'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="HolderssPivotTable")
End Sub
我在最后3行代码中遇到了与调用过程相关的调试问题,但我不确定原因。非常感谢帮助!
答案 0 :(得分:1)
Excel VBA无法接受Range
个对象中的StartPvt = sht.Name & "!" & sht.Range("A1").Address(ReferenceStyle:=xlR1C1)
单元格地址。
更改
StartPvt = sht.Range("A1").Address
到
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="HolderssPivotTable")
End Sub
然后改变
Set pvt = pvtCache.CreatePivotTable(TableDestination:=sht.Range(StartPvt), _
TableName:="HolderssPivotTable")
到
Holders (CORP)
更新 - 完整重构的代码,以确保数据透视表最终显示在Sub PivotTable()
Dim sht2 As Worksheet
Set sht2 = Sheets("Sheet2")
With sh2
Dim LastRow As Long
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Determine the data range you want to pivot
Dim SrcData As String
SrcData = .Name & "!" & .Range(.Cells(1, "A"), .Cells(LastRow, "E")).Address
End With
Dim sht As Worksheet
'Create a new worksheet
Set sht = Sheets("HOLDERS (CORP)")
'Where do you want Pivot Table to start?
Dim StartPvt As String
StartPvt = sht.Range("A1").Address
'Create Pivot Cache from Source Data
Dim pvtCache As PivotCache
Set pvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData)
'Create Pivot table from Pivot Cache
Dim pvt As PivotTable
Set pvt = pvtCache.CreatePivotTable(TableDestination:=sht.Range(StartPvt), TableName:="HolderssPivotTable")
End Sub
表格上。
picSchema.index({name: 'text', description: 'text', image: 'text'});