Alex Rivera | Logout

Charts Do Not Automatically Update When Data Changes

Asked 2010-12-16T22:50:47.250
12

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. When the data is updated by the VBA function the new numbers are not reflected in the charts that are pointing to them. I tried calling Application.Calculate, but that didn't do the trick. Any thoughts?


UDPATE:

I was able to duplicate this issue on a much smaller scale. Here's how:

  • Create a new workbook
  • Rename Sheet 1 to "Summary"
  • Rename Sheet 2 to "Data"
  • Open the Summary sheet in the VBA editor and paste the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
       If Target.Parent.Range("worksheetDate") = Target Then
          Application.CalculateFull
       End If
    End Sub
    
  • Create a new VBA module

  • Paste the following code into the new VBA module (I apologize - I can't get Stack Overflow to format this correctly for the life of me - this is the best I could get it to do):
    .

     Function getWeekValue (weekNumber As Integer, valuesRange As Range) As Integer   
    
     Dim aCell As Range  
     Dim currentDate As Date  
     Dim arrayIndex As Integer  
     Dim weekValues(1 To 6) As Integer  
    
     currentDate = ThisWorkbook.Names("worksheetDate").RefersToRange.Value
     arrayIndex = 1  
     For Each aCell In valuesRange 
         If month(currentDate) = month(ThisWorkbook.Sheets("Data").Cells( _  
                                       aCell.Row - 1, aCell.Column)) Then
             weekValues(arrayIndex) = aCell.Value 
             arrayIndex = arrayIndex + 1 
         End If 
     Next
    
     getWeekValue = weekValues(weekNumber)   
     End Function  
    

    .

  • Modify the Data worksheet to match the following image:

alt text

  • Select Cell B1 and name the range "work
Edit
Report

2 Answers

0

For example:

Sub a()
   Dim w As Worksheet
   Dim a
   Set w = Worksheets(1)

   For Each a In w.Range("a1:a5")
     a.Value = a.Value + 1
   Next

   w.ChartObjects(1).Chart.Refresh

End Sub  

alt text

answered 2010-12-17T00:03:55.753
-2

Just an idea: in your Worksheet_Change Sub, insert as the first line:

Application.EnableEvents = False

in order to avoid self-firing events....
Of course set it back to True at the end of the Sub.

answered 2010-12-17T22:11:35.363

Your Answer