Macro To Create New Workbook and Sheets

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I wonder if someone can help. In the macro below, I start in my main workbook and I make a copy of one of the sheets, called "Customer Report" to a new workbook. I then save this new workbook under the name "New Workbook", close it and therefore return to the main workbook.

I would like to add another task to this macro.

In the main workbook, I also have a worksheet called "Products".

What I would like to do is, once the new workbook has been created, I would like to then add a copy of the "Products" worksheet to the new workbook. And in addition, I would like to rename this worksheet as "Current Products". So the new workbook will have two sheets - "Customer Report" and "Current Products".

I have put where I think the new code will go (that's the easy part!).

Thanks,

MikeG

=================================================

Sub Create_New_Workbook()

Dim wbNew As Workbook, wbCur As Workbook


Set wbCur = ActiveWorkbook

ThisWorkbook.Worksheets("Customer Report").Copy

Set wbNew = ActiveWorkbook


'New code goes here I think

With wbNew
.SaveAs Filename:=Range("New Workbook") & ".xlsx"
End With

wbNew.Close

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Maybe (untested)

Code:
Sub Create_New_Workbook()
Dim wbNew As Workbook, wbCur As Workbook
Set wbCur = ActiveWorkbook
ThisWorkbook.Worksheets(Array("Customer Report", "Products")).Copy
Set wbNew = ActiveWorkbook
With wbNew
    .Sheets("Products").Name = "New Products"
    .SaveAs Filename:=Range("New Workbook") & ".xlsx"
End With
wbNew.Close
End Sub
 
Upvote 0
Maybe (untested)

Code:
Sub Create_New_Workbook()
Dim wbNew As Workbook, wbCur As Workbook
Set wbCur = ActiveWorkbook
ThisWorkbook.Worksheets(Array("Customer Report", "Products")).Copy
Set wbNew = ActiveWorkbook
With wbNew
    .Sheets("Products").Name = "New Products"
    .SaveAs Filename:=Range("New Workbook") & ".xlsx"
End With
wbNew.Close
End Sub

Thanks VoG

MikeG
 
Upvote 0
I have a related follow up question:

In the macro code below, when I get to the last line i thought it would put me into the original workbook - but it doesn't. Do I need some type of "Select" statement - e.g. curWB.Select?

Thanks

MikeG
==========================================

Sub Create_Customer_Report()

Dim wbNew As Workbook, wbCur As Workbook

Sheets("Customer Report").Select


Set wbCur = ActiveWorkbook
ThisWorkbook.Worksheets("Customer Report").Copy

Set wbNew = ActiveWorkbook

With wbNew
.SaveAs Filename:=Range("CR_File_Name") & ".xlsx"
End With

Set wbCur = ActiveWorkbook
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

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