1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lukasz
  • 152
  • 3
  • 16

2 Answers2

1

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

vacip
  • 5,246
  • 2
  • 26
  • 54
  • Thx Vacip. Works like a charm! DoEvents works. DoEvents+Sleep is dead. – Lukasz Jun 21 '15 at 12:25
  • You can try `application.wait` instead of `sleep`. The drawback is that the minimum time is one second there. The upside is that it is not dependant on CPU ticks (`sleep` is). – vacip Jun 21 '15 at 13:40
0

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
Mika72
  • 413
  • 2
  • 12