Hi there,
I don't think it's possible to save individual sheets of a workbook, but what you CAN do is to create a new workbook containing only the sheets you need & then save that workbook.
To do this, replace your code:
Application.EnableEvents = False
ActiveWorkbook.SaveCopyAs Filename:="C:\Data\" & _
Replace(ActiveWorkbook.Name, ".xls", _
" " & Format(Now, "yyyy-mm-dd-hh-mm") & ".xls")
Application.EnableEvents = True
with
and insert the following subroutine in your VBA module:
Option Explicit
Sub SaveWorkbook()
Dim strDummyName As String
Dim arySheets As Variant
Dim strSheet As Variant
Dim wbk As Workbook
Dim sht As Worksheet
arySheets = Array("Sheet1", "Sheet2")
On Error GoTo EnableAlerts
' The next line is just a unique sheetname - it can be anything!
strDummyName = "Ax2395EF"
Workbooks.Add
Set wbk = ActiveWorkbook
Application.DisplayAlerts = False
Do While wbk.Sheets.Count > 1
wbk.Sheets(1).Delete
Loop
Application.DisplayAlerts = True
wbk.Sheets(1).Name = strDummyName
For Each strSheet In arySheets
ThisWorkbook.Sheets(strSheet).Copy After:=wbk.Sheets(wbk.Sheets.Count)
Next strSheet
Application.DisplayAlerts = False
wbk.Sheets(strDummyName).Delete
Application.DisplayAlerts = True
Application.EnableEvents = False
On Error Resume Next
wbk.SaveAs Filename:="C:\Data\" & _
Replace(ThisWorkbook.Name, ".xls", " " & _
Format(Now, "yyyy-mm-dd-hh-mm") & ".xls")
If Err.Number <> 0 Then
On Error GoTo 0
MsgBox "Error encountered during ""Save"" operation - " & _
"the new workbook has NOT been saved", _
vbCritical, "Workbook not saved"
GoTo EnableAlerts
End If
wbk.Close SaveChanges:=False
EnableAlerts:
Application.EnableEvents = False
Application.DisplayAlerts = True
End Sub
Note that the statement:
arySheets = Array("Sheet1", "Sheet2")
contains the names of the sheets you want to include in the new workbook.
The routine works by creating a new workbook, deleting all of its worksheets except one (a workbook must contain at least one worksheet) & then renaming that worksheet with a unique "rubbish" name (just to avoid any conflict with the sheetnames in your source workbook.)
The routine then copies the specified worksheets (in the specified order) from the source workbook to the new workbook, and then deletes the worksheet with the "rubbish" name.
Finally, the new workbook is saved using your existing naming convention & is then closed.
Hope this helps - please let me know how you get on.
Best regards,
Greg M
Bookmarks