首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >同一工作表中的VBA -Place透视表

同一工作表中的VBA -Place透视表
EN

Stack Overflow用户
提问于 2016-08-25 01:14:41
回答 1查看 2.5K关注 0票数 2

VBA新手,我的宏用我的数据在同一个工作表上创建数据透视表时遇到了困难(例如,在"I1“列中启动数据透视表)。我已经能够运行宏来选择工作表中的所有数据,然后在另一个工作表上创建这些数据。由于我需要遍历同一工作簿上的多个工作表来创建多个透视表,因此使用单独的工作表是不可行的。

每个工作表的列数相同,但行数不同。我的目标是让这个宏查看每个工作表,并在它旁边输出一个数据透视表。

我觉得下面的宏没有引用正确的数据透视表目标。任何帮助都将不胜感激。

代码语言:javascript
运行
复制
Sub Macro4()  
'  
'Macro4 Macro  
'

'  
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row  
DataSheet = ActiveSheet.Name

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
DataSheet & "!R1C1:R" & FinalRow & "C8", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:=DataSheet & "!R1C9", TableName:= _
"PivotTable4", DefaultVersion:=xlPivotTableVersion15

Sheets(DataSheet).Select  
Cells(1, 9).Select  
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Document Type") 
    .Orientation = xlRowField.Position = 1  
End With  
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Accounting Event")    
    .Orientation = xlRowField.Position = 2  
End With  
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Document Number")
    .Orientation = xlRowField.Position = 3  
End With  
ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables
( _"PivotTable4").PivotFields("Amount"), "Sum of Amount", xlSum

End Sub  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-25 02:46:54

尝试下面的代码,它的方法略有不同,定义了RangePivotTablePivotCache,您可以根据需要轻松地修改它们:

代码语言:javascript
运行
复制
Sub Macro4()

Dim FinalRow            As Long
Dim DataSheet           As String
Dim PvtCache            As PivotCache
Dim PvtTbl              As PivotTable
Dim DataRng             As Range
Dim TableDest           As Range

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
DataSheet = ActiveSheet.Name

' set data range for Pivot Table
Set DataRng = Sheets(DataSheet).Range(Cells(1, 1), Cells(FinalRow, 8))  ' conversion of R1C1:R & FinalRow & C8

' set range for Pivot table placement
Set TableDest = Sheets(DataSheet).Cells(1, 9)  ' conversion of R1C9

Set PvtCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, DataRng)

' this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PvtTbl = ActiveWorkbook.Sheets(DataSheet).PivotTables("PivotTable4") ' check if "PivotTable4" Pivot Table already created (in past runs of this Macro)

On Error GoTo 0
If PvtTbl Is Nothing Then ' "PivotTable4" doesn't exist >> create it

    ' create a new Pivot Table in "PivotTable4" sheet
    Set PvtTbl = ActiveWorkbook.Sheets(DataSheet).PivotTables.Add(PivotCache:=PvtCache, TableDestination:=TableDest, TableName:="PivotTable4")

    With PvtTbl.PivotFields("Document Type")
        .Orientation = xlRowField
        .Position = 1
    End With

    With PvtTbl.PivotFields("Accounting Event")
        .Orientation = xlRowField
        .Position = 2
    End With

    With PvtTbl.PivotFields("Document Number")
        .Orientation = xlRowField
        .Position = 3
    End With

    PvtTbl.AddDataField ActiveSheet.PivotTables( _
    "PivotTable4").PivotFields("Amount"), "Sum of Amount", xlSum

Else
    ' just refresh the Pivot cache with the updated Range
    PvtTbl.ChangePivotCache PvtCache
    PvtTbl.RefreshTable
End If


End Sub
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39129358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档