You are here:

Excel/Copy three sheets to a new file

Advertisement


Question
Hi Jan,

Hope you are doing good.

Long back I had asked for you a code to copy two sheets to a new file and you had given me the below code, now I need a modification, I need to copy three sheets instead of two.

Sub CopyAndSaveSheets()
  Dim lCt As Long
  For lCt = 1 To ThisWorkbook.Worksheets.Count Step 2
      Worksheets(Array(ThisWorkbook.Worksheets(lCt).Name, ThisWorkbook.Worksheets(lCt + 1).Name)).Copy
      ActiveWorkbook.SaveAs ThisWorkbook.Worksheets(lCt).Name
      ActiveWorkbook.Close
  Next
End Sub

Best Regards,
Sanjay

Answer
If you want to copy sheets in sets of three (assuming the file contains a multiple of three sheets, so 3, 6, 9, 12, ... then this is the code you need:

Sub CopyAndSaveSheets()
  Dim lCt As Long
  For lCt = 1 To ThisWorkbook.Worksheets.Count Step 3
      Worksheets(Array(ThisWorkbook.Worksheets(lCt).Name, ThisWorkbook.Worksheets(lCt + 1).Name, _
         ThisWorkbook.Worksheets(lCt + 2).Name)).Copy
      ActiveWorkbook.SaveAs ThisWorkbook.Worksheets(lCt).Name
      ActiveWorkbook.Close
  Next
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jan Karel Pieterse

Expertise

Excel and Excel/VBA questions

Experience

Excel MVP

Organizations
Self employed Excel developer

Education/Credentials
Bachelor in Chemical Engineering

Awards and Honors
Microsoft MVP award since 2002

Past/Present Clients
Shell, Fortis bank, ABN-AMRO bank, Morgan Stanley, ...

©2016 About.com. All rights reserved.