I have a macro that works perfectly fine when I'm in the office and everything is quick. But it gives an error/crashes when I try to do it remotely because the connection is slower.
I think what's happening is, the macro moves onto the next step right after it connects to Access, rather than waiting until the pivot tables are actually refreshed (which takes time due to remote connection and Access having rather large amount of data.)
Is there any way to tell the macro not to move on until the pivot tables are fully refreshed?
Thanks.
Sub NewTemplates()
Dim i As Long
Dim k As Integer
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = ActiveSheet
k = ActiveSheet.Range("H1").End(xlDown).Row
MyPath = Range("MasterLocation").Value
Fcst = Range("FcstLocation").Value
Workbooks.Open Filename:=MyPath & "SGA Master Template.xlsx"
ActiveWorkbook.RefreshAll
For i = 1 To k
Range("_Office").Value = ws.Range("H" & i).Value
Range("_HCOffice").Value = ws.Range("H" & i).Value
ActiveWorkbook.SaveAs Filename:= _
Fcst & ws.Range("H" & i).Value & " Fcst " & ws.Range("FiscalYr").Value & ".xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
Next i
ActiveWindow.Close
MsgBox "New Templates Generated at " & Fcst, vbInformation
End Sub
The Range("_HCOffice") is actually a report filter of a pivot table that has the setting "Save source data with file" unchecked. Hence the macro gives an error message when it tries to change it before the table is refreshed.
I've set it up this way in order to save on file size when the template is saved.