I have an excel file which iterates over different excel files to update them and create a PDF file from them.
The macro iterates over rows and opens the files. This code opens the files:
Private Sub openFile(row As Integer)
Dim filePath As String
Dim wb As Workbook
filePath = Application.ActiveWorkbook.Path + "\" + allRows(row).filename
Set wb = Workbooks.Open(filePath)
Application.Run "RefreshEntireWorksheet"
Application.OnTime Now + (TimeSerial(0, 1, 59)), "'ThisWorkbook.updateCharts """ & row & "'"
Application.OnTime Now + (TimeSerial(0, 2, 59)), "'ThisWorkbook.createEmail'"
End Sub
Now if there are multiple files, only 1 file has the charts properly updated (the file being on the front). It seems like the updateCharts
code doesn't work at all:
Sub updateCharts(row As Integer)
Dim SheetName As String
Dim wb As Workbook
SheetName = "Sheet1"
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Set wb = Workbooks(allRows(row).filename)
wb.Activate
wb.Sheets(SheetName).Activate
For Each cht In wb.Sheets(SheetName).ChartObjects
cht.Chart.Refresh
DoEvents
Next cht
End Sub
I checked following links for answers, but none helped:
- Can't refresh a chart in Excel VBA (Excel 2016)
- Refresh all charts without blinking
- excel chart not updating when data changed by vba
- Excel 2013 vba refresh chart content after each iteration of For Loop
They all suggest to add an DoEvent
, but that doesn't work. So the question is, how can I update charts in a different workbook via VBA?
allRows
? i.e. what data type is it?. You shouldn't use things likeActivate
. If you qualify your workbook/worksheets, you don't need to activate them (unless you are doing that just to see what happens?) – ZacActivate
parts are already removed as they messed up the resulting PDF.allRows
is a made up property which simply refers to all the rows. Different columns are different properties. That part is working just fine, its just that charts are not properly updating. From what I gathered so far, it should be enough to trigger aCellChanged
event, as they aren't triggered on files in the background – XtremeBaumer