Charts Do Not Automatically Update When Data Changes

后端 未结 9 2872
庸人自扰
庸人自扰 2021-02-19 18:52

Hopefully this is an easy one. I have a series of charts in MS Excel that point to data on the same worksheet. The data on the worksheet is calculated using a VBA function. W

相关标签:
9条回答
  • 2021-02-19 19:47

    This solution worked for me. For the offending worksheet add:

    Private Sub Worksheet_Activate()
      Dim rngSelection          As Range
      Dim objChartObject        As ChartObject
      Dim objChart              As Chart
      Dim objSeriesCollection   As SeriesCollection
      Dim objSeries             As Series
      Dim strFormula            As String
    
      Set rngSelection = Selection
    
      For Each objChartObject In Me.ChartObjects
        Set objChart = objChartObject.Chart
        Set objSeriesCollection = objChart.SeriesCollection
        For Each objSeries In objSeriesCollection
          strFormula = objSeries.Formula
    
          objSeries.Delete
    
          Set objSeries = objSeriesCollection.NewSeries
    
          objSeries.Formula = strFormula
        Next objSeries
      Next objChartObject
    
      rngSelection.Select
    End Sub
    
    0 讨论(0)
  • 2021-02-19 19:49

    I've found that calling this Sub works...

    Sub DoAllEvents()
        DoEvents
        DoEvents
    End Sub
    

    BUT Microsoft cautions about being caught with the next DoEvents executing before the first DoEvents completes, which can happen depending on how often it's called without a delay between calls. Thus DoEvents appears to be acting as a type of non maskable interrupt, and nesting non maskable interrupts can cause the machine to freeze for multiple reasons without any recovery other than reboot.

    (Note: If one is not calling the routine above, often and quickly, nesting may not be an issue.)

    Using the following Sub below, which I modified from their suggestion, prevents this from happening.

    Sub DoAllEvents()
        On Error GoTo ErrorCheck
        Dim i
        For i = 1 To 4000    ' Start loop. Can be higher, MS sample shows 150000
            'I've found twice is enough, but only increased it to four or 4000.
            If i Mod 1000 = 0 Then     ' If loop has repeated 1000 times.
                DoEvents    ' Yield to operating system.
            End If
        Next i
        Exit Sub
    ErrorCheck:
        Debug.Print "Error: "; Error, Err
        Resume Next
    End Sub
    

    I appears that the number of DoEvents needed is based on the number of background tasks running on your machine, and updating the graph appears to be a background task for the application. I only needed two DoEvents because I call the routine frequently; however, I may end up upping it later if needed. I also keep the Mod at 1000 so to not change the lag between each DoEvents as Microsoft suggests, preventing nesting. One possible reason you might want to increase the number from 2000 to a higher number is if you system does not update the graph. Increasing this number allows the machine to handle larger numbers of background events that DoEvents might encounter through multiple calls as they are probably on a stack, and the DoEvents event is only allowed to run a specific number of cycles before marking its place in the stack to allow unhandled events and returning, leaving them to be handled on the next call. Thus the need for multiple calls. Changing this to their example of 150000 doesn't appear to slow the machine too much, to play it safe you might want to make it 150000.

    Note: the first example Sub with two DoEvents is probably safe depending on how often you call the Sub, however, if called too often, your machine might freeze up. Your call. ;-)

    PS: DoEvents will become one of your best calls if you create a lot of nested loops and the program doesn't behave as expected. Fortunately, this is available in all apps that use VBA!

    0 讨论(0)
  • 2021-02-19 19:51

    at the end of my changes I close the workbook and reopen it. that seems the easiest and most reliable way to update everything for me.

    0 讨论(0)
提交回复
热议问题