我正在尝试使用Excel Data创建一个powerpoint幻灯片。我基本上是创建了一个新的powerpoint幻灯片,然后使用vba将excel pivot数据添加到图表中。
但由于幻灯片中的许多图表,代码运行开始明显减慢,我需要在输入图表数据后关闭每个chartData窗口。
如果我尝试使用“ Workbook.Close ”关闭chartdata窗口,powerpoint上的图表不会考虑我输入到chartdata工作表的数据。不会更新图表。
此外,如果我没有关闭任何chartdata窗口,代码运行完美,幻灯片中的每个图表都会更新它应该是。
以下是示例代码:
Dim Pas_Shop_Chart As PowerPoint.Chart
Dim Pas_Shop_ChartData As ChartData
Dim Pas_Shop_Workbook As Excel.Workbook
Dim Pas_Shop_Worksheet As Excel.Worksheet
Dim Pas_ShopChartData(10, 4) As Variant
'Pas Shop Chart Data
Sheets("Pas Shop").Select
Sheets("Pas Shop").PivotTables("PivotTable2").PivotFields("REGION").ClearAllFilters
Sheets("Pas Shop").PivotTables("PivotTable2").PivotFields("REGION").CurrentPage = Region
Sheets("bayi baz").PivotTables("PivotTable2").PivotFields("Subs. Type").ClearAllFilters
Sheets("bayi baz").PivotTables("PivotTable2").PivotFields("Subs. Type").CurrentPage = "Pas"
Pas_Shop_Chart_Title = Range("d1").Value
For i = 1 To 10
Pas_ShopChartData(i, 1) = Sheets("Pas Shop").Cells(i + 5, 1)
Pas_ShopChartData(i, 2) = Sheets("Pas Shop").Cells(i + 5, 2)
Pas_ShopChartData(i, 3) = Sheets("Pas Shop").Cells(i + 5, 3)
Pas_ShopChartData(i, 4) = Sheets("Pas Shop").Cells(i + 5, 4)
Next i
'Creating Powerpoint Page
Set objPPApp = CreateObject("PowerPoint.Application")
objPPApp.Visible = msoTrue
Set objPresentation = objPPApp.Presentations.Add
Set objSlide = objPresentation.Slides.Add(1, ppLayoutBlank)
'Powerpoint Page Setup
With objPresentation.PageSetup
.SlideSize = ppSlideSizeCustom
.SlideWidth = 1140
.SlideHeight = 3130
End With
' PAS SHOP CHART
Set Pas_Shop_Chart = objSlide.Shapes.AddChart.Chart
Set Pas_Shop_ChartData = Pas_Shop_Chart.ChartData
Set Pas_Shop_Workbook = Pas_Shop_ChartData.Workbook
Set Pas_Shop_Worksheet = Pas_Shop_Workbook.Worksheets(1)
' PAS SHOP CHART DATA
Pas_Shop_Worksheet.ListObjects("Table1").Resize Pas_Shop_Worksheet.Range("A1:c11")
Pas_Shop_Worksheet.Range("Table1[[#Headers],[Series 1]]").Value = "Ratio"
Pas_Shop_Worksheet.Range("Table1[[#Headers],[Series 2]]").Value = "Region Med."
For i = 1 To 10
Pas_Shop_Worksheet.Cells(i + 1, 1).Value = Pas_ShopChartData(i, 1)
Pas_Shop_Worksheet.Cells(i + 1, 2).Value = Pas_ShopChartData(i, 4)
Cells(i + 1, 3).Value = Region_pas_general_perc2
Next i
Pas_Shop_Worksheet.Columns("B:C").Style = "Percent"
Pas_Shop_Worksheet.Columns("B:C").NumberFormat = "0.0%"
With Pas_Shop_Chart
.ChartGroups(1).GapWidth = 50
.ChartType = 51
.HasTitle = True
.ChartTitle.Text = Pas_Shop_Chart_Title
.ChartTitle.Format.TextFrame2.TextRange.Font.Name = "Arial"
.ChartTitle.Format.TextFrame2.TextRange.Font.Size = 18
.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Parent.Height = 290
.Parent.Width = 540
.Parent.Top = 1182.5
.Parent.Left = 582.5
.Legend.Delete
.Axes(xlCategory).TickLabels.Font.Size = 12
.Axes(xlCategory).TickLabels.Font.Name = "Calibri"
.Axes(xlValue).TickLabels.Font.Size = 12
.Axes(xlValue).TickLabels.Font.Name = "Calibri"
.ApplyDataLabels (xlDataLabelsShowValue)
.SeriesCollection(1).DataLabels.Font.Name = "Arial"
.SeriesCollection(1).DataLabels.Font.Size = 14
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.SeriesCollection(2).ChartType = xlLine
.SeriesCollection(2).Format.Line.Weight = 4.5
.SeriesCollection(2).Format.Line.DashStyle = msoLineRoundDot
.SeriesCollection(2).Format.Line.ForeColor.RGB = RGB(0, 0, 0)
.SeriesCollection(2).DataLabels.Delete
.FullSeriesCollection(2).Points(1).Select
.SetElement (msoElementDataLabelShow)
.SetElement (msoElementDataLabelLeft)
.SeriesCollection(2).DataLabels.Font.Name = "Arial"
.SeriesCollection(2).DataLabels.Font.Size = 14
.Axes(xlValue).MajorGridlines.Delete
.Axes(xlCategory).TickLabels.Font.Size = 11
.Axes(xlCategory).TickLabels.Font.Name = "Calibri"
.Axes(xlCategory).TickLabels.Font.Bold = True
.Axes(xlValue).Delete
.PlotArea.Left = 5
.PlotArea.Height = 225
.PlotArea.Top = 50
End With
我剪切到代码只显示一个图表的代码。我正在使用办公室2016.其他人遇到这个问题吗?
谢谢,