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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,213,489
Messages
6,113,952
Members
448,535
Latest member
alrossman

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