VBA to copy multiple worksheets within a workbook and export to a new workbook and save it

Jmorrison67

Board Regular
Joined
Aug 20, 2021
Messages
51
Office Version
  1. 2016
Platform
  1. Windows
Good afternoon MrExcel community,

I am currently looking for some help in adding multiple sheets into this code. It worked OK with one tab/worksheet added but when i try to add in multiple sheet names I get the 'Subscript out of range' error.

This is what currently works:

Sub test()
Sheets("Cover").Copy
ActiveWorkbook.SaveAs Filename:="c:\users\morrisonj\TEST22" & Format(Date, "ddmmyyyy") & ".xlsx"
End Sub

Output >> which is a new workbook with one tab called Cover
1632924226655.png




What I am trying to do is add more worksheets to run off at the same time. Worksheets names are:
Cover
Contents
Total Costs Summary
Customer Services - KPI's


So I tried researching on the forum and online and this code came up (amended to input my sheet names):

Sub test()
Sheets(Array("Cover", "Contents", "Total Costs Summary", "Customer Services - KPI's")).Copy
ActiveWorkbook.SaveAs Filename:="c:\users\morrisonj\TEST23" & Format(Date, "ddmmyyyy") & ".xlsx"
End Sub

I then get this error:
1632924502949.png


Debug:
1632924525702.png




My ultimate goal is to press a button to run off an extract from a workbook which has 80+ tabs and extract 4 tabs for one manager, 4 for another etc so aim to repeat the code by changing the sheet names.

Any help would be appreciated

Kind regards
Jmorrison67
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
68,339
Office Version
  1. 365
Platform
  1. Windows
Check the spelling of the sheet names, especially check for any leading/trailing spaces.
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
68,339
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,575
Messages
5,770,932
Members
425,652
Latest member
Pemby

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
Top