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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Check the spelling of the sheet names, especially check for any leading/trailing spaces.
 
Upvote 0
Solution
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

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