Excel 2013 vba refresh chart content after each iteration of For Loop

守給你的承諾、 提交于 2021-01-28 13:56:35

问题


Problem: Refresh graphical representaion of Series in ChartObject after each iteration of for loop

Ex. y=m*Cos(x) 
y - Values
m - parameter

I have some data counted from formula with parameter. I want to visualise what influence has the change of parameter on XYgraph. I want to do it in for loop (added Sleep do have some time to see results). Data and formula are in Excel SpreadSheet. Update script of parameter is in VBA module. Update works on values in spreadsheet but doesn't affect graph.

WorkBook.RefreshAll doesn't work; Chart.Refresh doesn't work

Chart updates after last iteration.

Any ideas?


回答1:


Add a DoEvents to your code after each iteration step. It works for me.




回答2:


In Excel for Office 365 I got XYChart live updating working only this way (XYChart to be updated during/in the end of each For loop iteration round). I could not get Sleep() working at all. Unfortunately Application.Wait tick resolution is 1 second, so the animation may be relatively slow. Also <chartObject>.Chart.Refresh did not have desired effect (for auto updating during the For loop).

Note: Some of the codes below may not be necessary in your case (at least Application.ScreenUpdating should be ON by default).

Application.ScreenUpdating = True    
ActiveSheet.ChartObjects(1).Activate

...

    For i = 1 To 10
       'Chart source data cells manipulation/update and other chart related routines here.'

       ActiveSheet.ChartObjects(1).Activate
       Application.Wait (Now + TimeValue("00:00:01"))
       DoEvents
   Next i


来源:https://stackoverflow.com/questions/30964263/excel-2013-vba-refresh-chart-content-after-each-iteration-of-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!