excel - 从特定列数据读取的 VBA 代码
问题描述
此 VBA 代码自动创建 PT 和图表。
我希望我的图表从“总计”列中读取。它默认并从第一列“关闭”中读取。
下面是我的代码:
Sub PT_C_RiskType_Status_test()
'
' PT_C_RiskType_Status_test Macro
'
'Defining the variables
Dim pc As PivotCache
Dim ws As Worksheet
Dim pt As PivotTable
Dim sh As Shape
Dim ch As Chart
Set pc = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:="DataSource", _
Version:=xlPivotTableVersion15)
Set ws = Sheets.Add
ws.Select
Cells(3, 1).Select
Range("A3:D9").Select
'Create the pivot table
Set pt = pc.CreatePivotTable(TableDestination:=ws.Name & "!R3C1", _
TableName:="Pivottable")
'Adding the data fields
pt.AddDataField _
Field:=pt.PivotFields("Status"), _
Function:=XlConsolidationFunction.xlCount
'Adding the fields
pt.AddFields _
RowFields:="Risk Type", _
ColumnFields:="Status"
'Adding chart
Set sh = ws.Shapes.AddChart2(XlChartType:=XlChartType.xlPie, _
Width:=200, Height:=200)
Set ch = sh.Chart
End Sub
请指教。
解决方案
推荐阅读
- node.js - 使用 multer 在 mongoDb 中存储和获取图像时遇到问题?
- sql - 从不同的表中选择数据并将计算插入到新表中
- docker - 重启后 Docker overlay2 文件丢失为空(Ubuntu 14.04)
- php - MySQL Text 数据类型的行为类似于 Int 数据类型
- spring - 当请求输入无效时,控制器周围的 Spring AOP 不起作用
- powershell - 如何使用 Powershell 将文件复制到不同的路径?
- ruby-on-rails - Rails:如何将 gem 的类保存为我的数据库中的模型?
- python - 使用列表理解进行列表转换
- networking - 代理链无法加载进程
- javascript - Nextjs 中的摇树 node_modules