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

lager1001

Board Regular
Joined
May 17, 2019
Messages
84
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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,780
Office Version
  1. 2007
Platform
  1. Windows
after I do a Data-Refresh All.


What do you mean by "after I do a Data-Refresh All".
Is it an excel command that you execute, is it when you update a specific cell?
 

Watch MrExcel Video

Forum statistics

Threads
1,132,803
Messages
5,655,397
Members
418,194
Latest member
LabraLime

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
Top