Refresh all charts without blinking

后端 未结 4 998
不思量自难忘°
不思量自难忘° 2020-12-11 21:37

The aim is to refresh all charts in Excel after cells recalculation.

I work with Microsoft Excel 2010.

As we know, there is a bug? in Excel so that Excel doe

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-11 22:06

    Happy Pi Day!

    I just did some experiments with animating charts, using VBA to change a counter in a cell, and worksheet formulas to recalculate chart data based on this counter.

    I used to do a lot of chart animations, back in the days of Excel 97-2003, and those ran pretty well. When Excel 2007 came out, the animations really degraded, and nothing seemed to help. But just now I did these tesst in the latest build of Office 365 (Version 1904, Build 11504). And it turns out, sometime in the past few years or so, Microsoft has made it work better.

    Sub ChartAnimation1()
      Dim i As Double
      For i = 0 To 1000 Step 50
        ActiveSheet.Range("Stepper") = i
      Next
    End Sub
    

    The animation didn't animate, that is, the chart didn't change despite the data changing.

    My experience told me I should put something like DoEvents in the code after I change the cell's value.

    Sub ChartAnimation2()
      Dim i As Double
      For i = 0 To 1000 Step 50
        ActiveSheet.Range("Stepper") = i
        DoEvents
      Next
    End Sub
    

    This helped a little, the chart changed, but the animation was not smooth. Some steps were missed, and the effect was a herky-jerky animation.

    Sub ChartAnimation3()
      Dim i As Double
      For i = 0 To 1000 Step 50
        ActiveSheet.Range("Stepper") = i
        DoEvents
        DoEvents
      Next
    End Sub
    

    This ran a bit more slowly than with one DoEvents, but it was a lot smoother; still not perfect, but pretty good.

    More than two DoEvents was overkill: the code took the same length of time, and the animation was not any smoother.

    I also tried various combinations of Chart.Refresh, Chart.Activate, and ScreenUpdating. Two takeaways:

    • Without a couple DoEvents, the animation didn't work regardless of what other things I tried.
    • With a couple DoEvents, none of these extra steps made the animation any smoother, but they could make it significantly slower.

    This was pretty interesting, so I'll blog about it some day. When I do I'll come back and post a link.

提交回复
热议问题