Solved

Creating a second workbook from an original workbook

Posted on 2013-01-29
6
285 Views
Last Modified: 2013-01-30
Workbook has 10 tabs. With the use of a macro a new workbook is created with 5 of the tabs.
The new workbook is saved to where the original is with a new name - if that is possible.
Any help greatly appreciated.
0
Comment
Question by:dgd1212
  • 4
  • 2
6 Comments
 
LVL 26

Expert Comment

by:redmondb
ID: 38833576
Hi, dgd1212.

What version of Excel are you using?
What file type do you want to use (xlsx/xlsm/xlsb/xls)?
Any particular name for the new file?

Thanks,
Brian.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38833598
dgd1212,

Please see previous post. I've made some assumptions...
 - The new file is saved as an xlsx.
 - Sheets2/4/6/8/10 are copied.
 - The output file is called "Fred.xlsx", unless that already exists, when the file is given the same name as the source file + "_yyyymmdd_hhmmss"

The code is...
Option Explicit

Sub Copy_Sheets()
Dim xBook As Workbook

Application.ScreenUpdating = False
    Sheets(Array("Sheet10", "Sheet8", "Sheet6", "Sheet4", "Sheet2")).Copy
    Set xBook = ActiveWorkbook

    If Dir(ThisWorkbook.Path & "\" & "Fred.xlsx") = "" Then
        xBook.SaveAs Filename:=ThisWorkbook.Path & "\" & ThisWorkbook.Name & "_" & Format(Now(), "yyyymmdd_hhnnss") & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    Else
        xBook.SaveAs Filename:=ThisWorkbook.Path & "\" & "Fred.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    End If
    
    xBook.Close savechanges:=False

Application.ScreenUpdating = True

End Sub

Open in new window

Regards,
Brian.
Copy-Sheets.xlsm
0
 

Author Comment

by:dgd1212
ID: 38833831
Brian,
I'm using Excel 2010. Type file would be xlsm (for macros?)
I used your attached example spreadsheet. Could not find the created file though. No fred.xlsx.
suggestions?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 38833864
Hi, dgd1212.

Please see attached. I've made a few changes...
 - Now saves as xlsm.
 - Corrected the check for an existing file.
 - Displays a message with the name of the file just saved.

The code is...
Option Explicit

Sub Copy_Sheets()
Dim xBook As Workbook
Dim xFileName As String

Application.ScreenUpdating = False
    Sheets(Array("Sheet10", "Sheet8", "Sheet6", "Sheet4", "Sheet2")).Copy
    Set xBook = ActiveWorkbook

    If Dir(ThisWorkbook.Path & "\" & "Fred.xlsm") <> "" Then
        xFileName = ThisWorkbook.Path & "\" & ThisWorkbook.Name & "_" & Format(Now(), "yyyymmdd_hhnnss") & ".xlsm"
        xBook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    Else
        xFileName = ThisWorkbook.Path & "\" & "Fred.xlsm"
        xBook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    End If

    xBook.Close savechanges:=False

Application.ScreenUpdating = True

MsgBox ("Sheets saved to " & xFileName)

End Sub

Open in new window

Regards,
Brian.Copy-Sheets-V2.xlsm
0
 

Author Closing Comment

by:dgd1212
ID: 38835387
Brian,
Thank you!
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38835654
Glad to help, dgd1212!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
My experience with Windows 10 over a one year period and suggestions for smooth operation
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question