简体   繁体   中英

Excel VBA To Split Workbook Every 4 Worksheets

I have a workbook that contains about 100 worksheets. I want to create 25 individual workbooks that contain 4 sheets each. I am trying to write an Excel VBA foor loop that will do such - but it is generating a workbook for every 1 page.

totalPages = PDDoc.GetNumPages

For i = 1 To totalPages - 4 
    'This is where you would add the workbook and transfer the page
Next i
Sub SplitPages()
   Dim i As Integer

   Dim wb As Workbook

   For i = 1 To ThisWorkbook.Worksheets.Count
        If i Mod 4 = 1 Then
           Set wb = Workbooks.Add
        End If
       ThisWorkbook.Worksheets(i).Copy wb.Worksheets(1)
    Next i
End Sub

You have to copy to avoid messing up the count, and the new workbooks have an extra sheet - but a quick

  for each wb in workbooks:wb.sheets("sheet1").delete:next wb

will fix that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM