I am attempting to create a macro which opens up a file, refresh data from a SAP BW query and will then save and close.
Currently the macro initiates the refresh however it moves on to the Save and close command before it has finished and therefore nothing changes.
I have seen that there are ways off 'pausing' or 'sleeping' for a period of time to allow the command to be completed however I wish to expand this macro to opening multiple workbooks with queries which take differing times to refresh so therefore that would be a last resort. I have currently used DoEvents
however this doesn't seem to be working either.
Note: the refresh works through SAP BEx Analyzer 7.
My code:
Sub OpenAndRefresh()
Workbooks.Open "QueryRefresh.xls", UpdateLinks:=False
Workbooks("QueryRefresh.xls").Activate
Run "BExAnalyzer.XLA!SAPBEXrefresh", True
DoEvents
Workbooks("QueryRefresh.xls").Close SaveChanges:=False
End Sub
Any help or guidance would be greatly appreciated.
I have a macro for this purpose, and I've never had this issue:
Public Sub Refresh_All()
Dim filepathstr As String
Dim filename As String
Dim wbk As Workbook
filepathstr = Sheet1.Range("filepath").Value
For Each cell In Sheet1.Range("workbooks")
If Not cell.Value = "" Then
filename = cell.Value
Set wbk = Workbooks.Open(filepathstr & filename)
''''**REFRESH**''''''
SAPBexrefresh (True)
Application.DisplayAlerts = False
wbk.Save
wbk.Close False
Application.DisplayAlerts = True
End If
Next cell
End Sub
The main difference here code-wise is that I'm calling the refresh macro directly rather than using the Run command.
EDIT: To make this work you also need to add the "BExAnalyzer" Reference in your project. If you're not familiar with adding references, you need to go into Tools --> References, then click on "BExAnalyzer" out of the long list of available references.
I'm not sure why this would make any difference but as I say, I've always found that my macro finishes refreshing (even when this takes up to 15 minutes) before continuing. I'm also using BEx 7