Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying 2 worksheets to a new workbook, and saving 2

Status
Not open for further replies.

t16turbo

Programmer
Mar 22, 2005
315
GB
Hi there,

I have a macro that generates all sorts of wonderful things.
I need to copy 2 worksheets "ConsolidatedFOB" and "Capacity and Headcount" into a new workbook.

These need to be saved without any macros - (kind of like a copy and paste special into the new book).

Can anyone suggest a clean and easy way of achieving this?
 
Hello t16turbo,

I would try recording the act of copying, pasting, and saving with the macro recorder. The resulting code could be modified to fit your application, maybe like the following.

Code:
Option Explicit

Sub SaveWorkSheets()
    Dim oOldWorkbook As Workbook
    Set oOldWorkbook = ActiveWorkbook
    
    Dim oNewWorkBook As Workbook
    Set oNewWorkBook = Workbooks.Add
    
    Dim oOldSh As Worksheet
    Set oOldSh = oOldWorkbook.Worksheets("ConsolidatedFOB")
    oOldSh.Cells.Copy
    
    Dim oNewSh As Worksheet
    Set oNewSh = oNewWorkBook.Worksheets("Sheet1")
    oNewSh.Name = "ConsolidatedFOB"
    oNewSh.Range("A1").Activate
    oNewSh.Paste
    
    oNewWorkBook.SaveAs Filename:="ConsolidatedFOB"

End Sub

You might need the PasteSpecial method instead of Paste, and you might prefer not to copy all of the cells in the sheet... but is this the kind of thing that you are looking for?

Best Regards,
Walter
 
t16turbo,

Sorry, I forgot clean-up again.

Code:
...
    Set oOldSh = Nothing
    Set oOldWorkbook = Nothing
    
    Set oNewSh = Nothing
    Set oNewWorkBook = Nothing

End Sub

Best Regards,
Walter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top