[英]Add data and format Pivot Chart using VBA Excel
I am struggling to format a Pivotchart
& add data to the chart using VBA. 我正在努力格式化数据
Pivotchart
图并使用VBA将数据添加到图表中。 I have setup the code to create the pivot chart which is working fine and I can manually format & add data but whenever I try and mimic the macro recorder or anything I have read online it seems to fail stated 'Application-defined or object defined error.' 我已经设置了代码以创建能够正常工作的数据透视图,并且可以手动设置格式并添加数据,但是每当我尝试模仿宏记录器或我在网上阅读的任何内容时,它似乎都会失败,提示“应用程序定义或对象定义的错误。”
I have tried to change the code to change objChart
to ActiveChart
trying to mimic what I done when using the macro recorder but this still fails. 我试图更改代码以将
objChart
更改为ActiveChart
以模仿我在使用宏记录器时所做的事情,但这仍然失败。 I tried the recorder after my initial attempts failed. 最初的尝试失败后,我尝试了录音机。
I have also failed to have the ChartTitle.Add
work so I can amend the title that is visible (Currently nothing shows as a title). 我还没有
ChartTitle.Add
工作,因此我可以修改可见的标题(当前没有显示任何标题)。
ActionTracker.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Actions, Version:=6).CreatePivotTable _
TableDestination:=CtyDrng, TableName:="Country Overview", _
DefaultVersion:=6
Dim pt As PivotTable
Dim PtRange As Range
Dim ChrtRange As Range
Dim objChart As Chart
Dim chtSeries As SeriesCollection
Set pt = CountryDash.PivotTables("Country Overview")
Set PtRange = pt.TableRange1
With PtRange
Set ChrtRange = .Offset(, 1).Resize(1, .Columns.Count - 1)
Set ChrtRange = Union(ChrtRange, .Offset(.Rows.Count - 1, 1).Resize(1, .Columns.Count - 1))
End With
Set objChart = CountryDash.Shapes.AddChart.Chart
With objChart
.SetSourceData ChrtRange
.ChartType = xlBarStacked100
.ShowAllFieldButtons = True
' .ChartTitle.Add
'.ChartTitle.Select
' .ChartTitle.Text = "Country Overview"
End With
With objChart.PivotLayout.PivotTable.PivotFields("Country")
.Orientation = xlRowField
.Position = 1
End With
With objChart.PivotLayout.PivotTable.PivotFields("Region")
.Orientation = xlRowField
.Position = 2
End With
With objChart.PivotLayout.PivotTable.PivotFields("Overdue")
.Orientation = xlRowField
.Position = 3
End With
objChart.PivotLayout.PivotTable.AddDataField objChart.PivotLayout. _
PivotTable.PivotFields("Overdue"), "Overdue", xlCount
I'm expecting the code to create a pivot chart by country then region showing the overdue actions with 3 different categories 'Implemented', 'In Progress' & 'Overdue'. 我希望代码能按国家/地区创建一个数据透视图,然后按区域显示“逾期”行动,其中有3个不同类别“已实施”,“进行中”和“逾期”。
Pardon if this doesn't fit perfectly, but I haven't attempted to use the adddatafield
, which I assume is causing an issue since you're adding to a chart, not the pivot cache. 如果这不完全适合,请原谅,但我没有尝试使用
adddatafield
,因为您要添加到图表而不是数据透视表缓存,所以我认为这会导致问题。
You can avoid that by associating with a pivotfield which is already available in the pivotcache (called by header): 您可以通过与数据透视表缓存中已经可用的数据透视表字段关联(通过标头调用)来避免这种情况:
With pivotsheet.PivotTables("CHART_NAME")
'Insert Row Fields
With .PivotFields("Country")
.Orientation = xlRowField
End With
'Insert Data Field
With .PivotFields("Overdue")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Number Overdue"
End With
End With
To your point of adding a name to the pivottable, think about how you're generating... don't just append things, action as they occur such that: 在将名称添加到数据透视表的过程中,请考虑一下您是如何生成的...不要仅仅在事物发生时附加事物,动作,例如:
'Define Pivot Cache
lrs = source.Cells(source.Rows.Count, 1).End(xlUp).Row
Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="SOURCE_SHEET!A1:B" & lrsource)
'Insert Blank Pivot Table
Set pt = pc.CreatePivotTable(TableDestination:=pivotsheet.Cells(1, 1), TableName:="CHART_NAME")
After you've added the pivot table start adding the page fields, the column fields, the rows, and data fields. 添加数据透视表后,开始添加页面字段,列字段,行和数据字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.