I've created a macro which should refresh all data sources. It's data sources are sql servers, and as such automatically pull up the password box as required. If you've already input a password into the server since Excel was last opened it doesn't ask for the password.
I've managed to get the following piece of code together, but it's not behaving as I'd expect
Sub BSR_Refresher()
'Refreshes the spreadsheet and copies it with today's date
'Clears all filters
On Error Resume Next
ActiveWorkbook.ShowAllData
'Refreshes Spreadsheet
For Each objConnection In ThisWorkbook.Connections
'Get current background-refresh value
bBackground = objConnection.OLEDBConnection.BackgroundQuery
'Temporarily disable background-refresh
objConnection.OLEDBConnection.BackgroundQuery = False
'Refresh this connection
objConnection.Refresh
'Set background-refresh value back to original value
objConnection.OLEDBConnection.BackgroundQuery = bBackground
Next
'Saves Spreadsheet
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\\Company.local\AnyDrive\Company\Projects\Project001\Reporting\Report Updates" & Format(Date, ddmmyyyy) & ".xls"
End Sub
From my knowledge of VBA this should do the following:
1) Clear all filters from the tables
2) Run a data refresh (cribbed from Here)
3) Save to \\Company.local\AnyDrive\Company\Projects\Project001\Reporting\Report Updates
(fake names, actual structure) with the file name as FileName
08/07/2015 (where FileName
is the current name of the file)
Any clues as to why this is?
EDIT:
As per comments, its not saving the documents as I require.
==================
I've altered the code and it's still not working. I've moved things around as the loop was leading to repeated deletion of one of the sheets due to the addition of a "delete sheet" step.
Sub BSR_Refresher()
'Refreshes the spreadsheet and copies it with today's date
' Gets name to save new workbook as
Dim StrSaveName As String
Dim StrFolderPath As String
StrSaveName = "Report" & Format(Date, ddmmyyyy) & ".xlsx"
StrFolderPath = "\\Company.local\anyDrive\Company\Projects\Project-001\Reporting\Status Report Updates\"
StrSaveAs = StrFolderPath & StrSaveName
'Deletes Sheet1, Clears all filters
Application.DisplayAlerts = False
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
'Refreshes Spreadsheet
On Error Resume Next
ActiveWorkbook.ShowAllData
For Each objConnection In ThisWorkbook.Connections
'Get current background-refresh value
bBackground = objConnection.OLEDBConnection.BackgroundQuery
'Temporarily disable background-refresh
objConnection.OLEDBConnection.BackgroundQuery = False
'Refresh this connection
objConnection.Refresh
'Set background-refresh value back to original value
objConnection.OLEDBConnection.BackgroundQuery = bBackground
Next
'Saves Spreadsheet
ActiveWorkbook.SaveAs Filename:=StrSaveAs
End Sub
My issue is that it doesn't seem to save to where it needs to be :S