Solved

Creating a second workbook from an original workbook

Posted on 2013-01-29
6
269 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

803 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