Excel 图表未正确更新 VBA
Excel Chart not updating correctly with VBA
所以我制作了一个宏来导出图表,然后将其作为图片导入到用户窗体中。 Import/export 从那时起开始工作,但每次我打开工作簿时,当数据更改时图表不会更新并且看起来不同。当我手动删除数据系列并重新输入系列时,一切又正确了。但是,当我尝试让宏执行此操作时,它不会更新。我在本地使用 Office365,也尝试过 运行 Application.Screenupdating=True
的代码
USERFORM 中的完整代码下方 - 我正在做的是,我将图表放在一个 sheet 上,通常是隐藏的(在宏执行期间可见)然后 exports/imports将单个图表作为图片放入用户窗体图像控件中。
Sub ChangeChart(ChartName As String)
Dim CurrentChart As Chart
Dim CName As String
Dim iCS As Integer
'//////////////LOADS IN THE DIFFERENT CHARTS//////////////
'Code Optimize
Set CurrentChart = wksJ.ChartObjects(ChartName).Chart 'Selects chart from wksJ
'Validates Chart Data
Select Case ChartName
Case "PieTotal"
CurrentChart.FullSeriesCollection(1).Delete
CurrentChart.SetSourceData Source:=Range("AG5:AH13")
CurrentChart.SetElement (msoElementDataLabelCallout)
Case "TrendOverall"
For iCS = 1 To 9
CurrentChart.FullSeriesCollection(1).Delete
Next iCS
CurrentChart.SetSourceData Source:=Range("AR5:BA22")
CurrentChart.SetElement (msoElementDataLabelLeft)
Case "BarMonthly"
For iCS = 1 To 9
CurrentChart.FullSeriesCollection(1).Delete
Next iCS
CurrentChart.SetSourceData Source:=Range("AG29:AP47")
Case "PieAtt"
CurrentChart.FullSeriesCollection(1).Delete
CurrentChart.SetSourceDataSource:=wksJ.Range(Range("AR29"), Range("AR29").End(xlDown).Offset(, 1))
CurrentChart.SetElement (msoElementDataLabelCallout)
End Select
'Exports and Loads in the charts
CName = ThisWorkbook.Path & "\temp.jpg" 'Sets path for chart pic export and names it temp
CurrentChart.Export Filename:=CName, filtername:="jpg" 'Exports chart as JPG to path destination
ufStatistics.imgStat.Picture = LoadPicture(CName) 'Loads GIF into ufStatistics
End Sub
这是图表的样子,如果我使用代码更新 SourceData
但是,如果我手动设置 range/source 数据,即我手动删除系列并重新选择相同的 运行ge,图表看起来正确
我尝试录制手动选择的源数据,但是当我运行录制宏时,它给出了同样的错误结果。查看以下录音结果:
ActiveSheet.Shapes("PieTotal").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveChart.FullSeriesCollection(1).Delete
Application.CutCopyMode = False
ActiveChart.SetSourceData Source:=Range("AG5:AH13")
好吧,我终于找到了答案,多亏了适量的杜松子酒;P ...答案其实很简单(它总是生活中的小事)-将.Value
添加到xValues范围:
With CurrentChart
.FullSeriesCollection(1).Delete
.SeriesCollection.NewSeries
.SetSourceData Source:=wksJ.Range("=JOB!$AG:$AH"), PlotBy:=xlColumns
.SeriesCollection(1).XValues = wksJ.Range("AG5:AG13").Value
.SetElement (msoElementDataLabelCallout)
End With
所以我制作了一个宏来导出图表,然后将其作为图片导入到用户窗体中。 Import/export 从那时起开始工作,但每次我打开工作簿时,当数据更改时图表不会更新并且看起来不同。当我手动删除数据系列并重新输入系列时,一切又正确了。但是,当我尝试让宏执行此操作时,它不会更新。我在本地使用 Office365,也尝试过 运行 Application.Screenupdating=True
USERFORM 中的完整代码下方 - 我正在做的是,我将图表放在一个 sheet 上,通常是隐藏的(在宏执行期间可见)然后 exports/imports将单个图表作为图片放入用户窗体图像控件中。
Sub ChangeChart(ChartName As String)
Dim CurrentChart As Chart
Dim CName As String
Dim iCS As Integer
'//////////////LOADS IN THE DIFFERENT CHARTS//////////////
'Code Optimize
Set CurrentChart = wksJ.ChartObjects(ChartName).Chart 'Selects chart from wksJ
'Validates Chart Data
Select Case ChartName
Case "PieTotal"
CurrentChart.FullSeriesCollection(1).Delete
CurrentChart.SetSourceData Source:=Range("AG5:AH13")
CurrentChart.SetElement (msoElementDataLabelCallout)
Case "TrendOverall"
For iCS = 1 To 9
CurrentChart.FullSeriesCollection(1).Delete
Next iCS
CurrentChart.SetSourceData Source:=Range("AR5:BA22")
CurrentChart.SetElement (msoElementDataLabelLeft)
Case "BarMonthly"
For iCS = 1 To 9
CurrentChart.FullSeriesCollection(1).Delete
Next iCS
CurrentChart.SetSourceData Source:=Range("AG29:AP47")
Case "PieAtt"
CurrentChart.FullSeriesCollection(1).Delete
CurrentChart.SetSourceDataSource:=wksJ.Range(Range("AR29"), Range("AR29").End(xlDown).Offset(, 1))
CurrentChart.SetElement (msoElementDataLabelCallout)
End Select
'Exports and Loads in the charts
CName = ThisWorkbook.Path & "\temp.jpg" 'Sets path for chart pic export and names it temp
CurrentChart.Export Filename:=CName, filtername:="jpg" 'Exports chart as JPG to path destination
ufStatistics.imgStat.Picture = LoadPicture(CName) 'Loads GIF into ufStatistics
End Sub
这是图表的样子,如果我使用代码更新 SourceData
但是,如果我手动设置 range/source 数据,即我手动删除系列并重新选择相同的 运行ge,图表看起来正确
我尝试录制手动选择的源数据,但是当我运行录制宏时,它给出了同样的错误结果。查看以下录音结果:
ActiveSheet.Shapes("PieTotal").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveChart.FullSeriesCollection(1).Delete
Application.CutCopyMode = False
ActiveChart.SetSourceData Source:=Range("AG5:AH13")
好吧,我终于找到了答案,多亏了适量的杜松子酒;P ...答案其实很简单(它总是生活中的小事)-将.Value
添加到xValues范围:
With CurrentChart
.FullSeriesCollection(1).Delete
.SeriesCollection.NewSeries
.SetSourceData Source:=wksJ.Range("=JOB!$AG:$AH"), PlotBy:=xlColumns
.SeriesCollection(1).XValues = wksJ.Range("AG5:AG13").Value
.SetElement (msoElementDataLabelCallout)
End With