Save multiple sheets in a new workbook

Dave_george

New Member
Joined
Jul 20, 2023
Messages
32
Office Version
  1. 2021
  2. 2016
  3. 2013
Platform
  1. Windows
I have a workbook with the multiple sheets (Sheet1, Sheet2 .. Sheet 5) and I want to create a macro assigned button to Save as a new work book containing only Sheet 1, Sheet 2 and Sheet3. On clicking the Macro assigned button the Save as dialogue box should pop up allowing the user to select destination and also optionally a new file name. I am also saving the file as an .xlsx format. Can anyone help me with the code?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I have got a code that does this however it also insets a blank sheet called sheet1.

VBA Code:
Sub SaveSelectedSheetsAsNewWorkbook()
    Dim wb As Workbook
    Dim newWb As Workbook
    Dim ws As Worksheet
    Dim savePath As String
    
    ' Set the save path and filename
    savePath = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx")
    
    ' Exit if user cancels the save dialog
    If savePath = "False" Then Exit Sub
    
    ' Create a new workbook
    Set newWb = Workbooks.Add
    
    ' Loop through the selected sheets and copy them to the new workbook
    For Each ws In ThisWorkbook.Sheets(Array("Sheet6", "Sheet2"))
        ws.Copy After:=newWb.Sheets(newWb.Sheets.Count)
    Next ws
    
    ' Save and close the new workbook
    newWb.SaveAs savePath
    newWb.Close
    
    ' Clean up
    Set newWb = Nothing
End Sub
 
Upvote 0
Put a breakpoint on the For-Each line and when the code stops there have a look at the workbook that was just added.
A new workbook automatically has a sheet1.
The sheet could be hidden or deleted prior to saving.
 
Upvote 0
Or maybe this will do
VBA Code:
Sub SaveSheetsAsNewWorkbook()
    
    Dim savePath As String
    
    ' Set the save path and filename
    savePath = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx")
    
    ' Exit if user cancels the save dialog
    If savePath = "False" Then Exit Sub
    
    ThisWorkbook.Sheets(Array("Sheet6", "Sheet2")).Copy
    
    ' Save and close the new workbook
    ActiveWorkbook.SaveAs savePath
    ActiveWorkbook.Close

End Sub
 
Upvote 1
Solution
Or maybe this will do
VBA Code:
Sub SaveSheetsAsNewWorkbook()
   
    Dim savePath As String
   
    ' Set the save path and filename
    savePath = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx")
   
    ' Exit if user cancels the save dialog
    If savePath = "False" Then Exit Sub
   
    ThisWorkbook.Sheets(Array("Sheet6", "Sheet2")).Copy
   
    ' Save and close the new workbook
    ActiveWorkbook.SaveAs savePath
    ActiveWorkbook.Close

End Sub
Thank you. This worked.
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top