Solved

Creating a second workbook from an original workbook

Posted on 2013-01-29
6
247 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Outlook Free & Paid Tools
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now