excel chart not updating when data changed by vba

偶尔善良 提交于 2021-01-29 04:41:24

问题


I have a Bubble chart that I am trying to add "motion" to by having a macro update the table and subsequently the chart. I have a cell that I use as the "offset" which is used by my data table to get the data. I have a button that runs a VBA macro that updates this "offset" cell for each month in my data, which updates the data table when the offset updates.

When I alter the offset cell manually, both the table and chart update. However, when I click the button and run the VBA macro, only the table updates, NOT the graph.

I have looked researched possible solution, including those items located here on Stack Overflow, and have tried the following:

-DoEvents
-Applications.Calculate
-ActiveWorkbook.RefreshAll
-Chart.Refresh
-setting the Application.ScreenUpdating to false and back to true

Here is my VBA code:

Sub Button7_Click()
Dim i As Integer

For i = 0 To 9:
    Range("P1").Value = i
    Application.Calculate
    Application.Wait DateAdd("s", 1, Now)
Next
Range("P1").Value = 0
End Sub

It shouldn't be this hard to update a graph when the table updates via VBA.


回答1:


Based on my test, the following code works on my side:

Sub Button7_Click()
Dim i As Integer
Dim sheet As Worksheet
Set sheet = Worksheets("Sheet10")

ActiveSheet.EnableCalculation = True
Application.ScreenUpdating = True
For i = 1 To 9
   'sheet.Range("A1") = CStr(i)
   sheet.Cells(i, 1) = CStr(i)
    'Application.Wait (DateAdd("s", 1, Now))
Next

sheet.UsedRange.Calculate

ActiveSheet.EnableCalculation = False
Range("A1").Value = 0
End Sub



回答2:


I've had this problem. Seems related with Excel 2016.

Sub CommButton1_Click()
i = Cells(4, 24)
start = Cells(4, 22)
rango = Cells(4, 23) + start
Do Until start > rango
    Sleep (20)
    Cells(4, 25).Value = start
    start = start + i
    DoEvents
    DoEvents
Loop
End Sub

The soultion seems to be to add another DoEvents. I've tried in Excel 2016 and it worked, but with a big time delay.



来源:https://stackoverflow.com/questions/51957981/excel-chart-not-updating-when-data-changed-by-vba

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