Macro that saves two worksheets from a workbook and save them as a new workbook

BenScadden

New Member
Joined
Jun 11, 2007
Messages
4
I would greatly appreciate a macro that can be used, on a daily basis, to copy two sheets from a workbook (as values), save them as a new workbook to a specific location and name them with the current date.

My workbook is called 'Daily Cash Management'. The two pages I would like to copy from this are called 'NW Bank Bal' & 'Other Bank Bal'.

I would like them to be saved to following path 'S:\Cash Management\Cash Management Sheets\Daily Sheets\DCM Bank Bal Pages

Ideally the new file will be named with the full date for the day in question. So for today '25062008' - in English date format.

I intend on using this every day to save these two sheets so that I will have a copy of them for each day.

I am using Excel 2002.

Many thanks in advance.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
park this type of a macro in the work book "cash management"

Code:
Option Explicit
Sub test()


Sheets(Array("Sheet1", "Sheet2")).Copy
ActiveWorkbook.SaveAs ("e:\excel\new")

End Sub

in such simple cases you start a macro and do the steps and view the macro and you can generalise it. This will be a good exercise.
 
Upvote 0
Building on venkat's code a little, and adding a little error-checking:
Code:
Sub test()
Const strWS1 As String = "NW Bank Bal"
Const strWS2 As String = "Other Bank Bal"
Const strPath As String = "S:\Cash Management\Cash Management Sheets\Daily Sheets\DCM Bank Bal Pages"

'Date for new file name
strPath = strPath & "\" & Format(Now, "ddmmyyyy") & ".xls" '2003 Excel Sheet
Debug.Print strPath

'Copy sheets
Sheets(Array(strWS1, strWS2)).Copy

'Save workbook
If MyFileExists(strPath) Then
    MsgBox "File exists.  This routine will not overwrite the existing file.  Execution aborted."
Else
    ActiveWorkbook.SaveAs strPath
End If

End Sub
'---------------------------------------------------------
Public Function MyFileExists(strPath As String) As Boolean

If Dir(strPath) > "" Then
    MyFileExists = True
Else
    MyFileExists = False
    MsgBox strPath & " already exists."
End If

End Function

I wasn't sure if you want the date appended to the existing file name or just use the date by itself as the file name - above the latter course is followed. This can be easily changed.

Regards


Edit: Note I very rarely used excel 2002 but I think the file extension is the same as for 2003, i.e. ".xls" - correct? AB
 
Upvote 0

Forum statistics

Threads
1,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

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