gpt4 book ai didi

Excel 图表无法使用 VBA 正确更新

转载 作者:行者123 更新时间:2023-12-04 20:48:37 26 4
gpt4 key购买 nike

所以我制作了一个宏来导出图表,然后将其作为图片导入用户表单。导入/导出工作正常,当数据更改时图表不会更新,并且每次打开工作簿时看起来都不同。当我手动删除数据系列并重新输入系列时,一切都恢复正常。但是,当我尝试让宏来执行此操作时,它并没有更新。我在本地使用 Office365,也尝试使用 Application.Screenupdating=True 运行代码
在用户表单中的整个代码下方 - 我正在做的是,我将图表放在一张纸上,通常是隐藏的(在宏执行期间可见),然后将单个图表作为图片导出/导入到用户表单图像控件中。

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,这就是图表的外观
enter image description here
enter image description here
但是,如果我手动设置范围/源数据,即我手动删除系列并重新选择相同的范围,图表看起来正确
enter image description here
enter image description here
我尝试记录手动选择源数据,但是当我运行宏记录时,它给出了同样的错误结果。看下面的录音结果:
    ActiveSheet.Shapes("PieTotal").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveChart.FullSeriesCollection(1).Delete
Application.CutCopyMode = False
ActiveChart.SetSourceData Source:=Range("AG5:AH13")

最佳答案

好的,多亏了适量的 gin&tonic ,我终于找到了答案 ;P ...答案其实很简单(生活中的小事总是如此)- 加.Value到 xValues 范围:

With CurrentChart
.FullSeriesCollection(1).Delete
.SeriesCollection.NewSeries
.SetSourceData Source:=wksJ.Range("=JOB!$AG$5:$AH$13"), PlotBy:=xlColumns
.SeriesCollection(1).XValues = wksJ.Range("AG5:AG13").Value
.SetElement (msoElementDataLabelCallout)
End With

关于Excel 图表无法使用 VBA 正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72002719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com