VBA - Save Copy of Worksheet to Specific File Path AFTER Data Refresh

lager1001

Board Regular
Joined
May 17, 2019
Messages
88
To the VBA experts:

I need VBA code that will take the active worksheet (Sheet1), the only worksheet in the workbook, and copy and paste the values into a new workbook and save to a specified directory/folder, after I do a Data-Refresh All. It's essentially archiving a copy each time the data is refreshed.

I have the following code but I need it coded properly to be triggered upon the event of the refresh. Any ideas?

Sub SaveCopy()
Dim wsCopy As Worksheet, wsPaste As Worksheet
Dim wb As Workbook
Dim sFileName As String, sPath As String






'Path to store new file
sPath = "FilePath/Filepath/Filepath"
'Change filename as required
sFileName = "Archived_Report " & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xlsx"



'set the sheet you are copying. Change where neccessary
Set wsCopy = ThisWorkbook.Worksheets("Sheet1")
Set wb = Workbooks.Add
Set wsPaste = wb.Sheets(1)

'Copy everything from copy sheet
wsCopy.Cells.Copy
'Paste Values only
wsPaste.Cells.PasteSpecial xlPasteFormats
wsPaste.Cells.PasteSpecial xlPasteValues
Application.CutCopyMode = False


'Save new workbook

wsPaste.Name = "Sheet1" 'Change if needed
wb.SaveAs FileName:=sPath & sFileName, FileFormat:=xlOpenXMLWorkbook
MsgBox ("Report Archived")
wb.Close


End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top