我有一个包含大约100个工作表的工作簿。我想创建25个单独的工作簿,每个工作簿包含4张。我正在尝试编写一个可以执行此操作的Excel VBA foor loop
,但它每1页生成一个工作簿。
totalPages = PDDoc.GetNumPages
For i = 1 To totalPages - 4
'This is where you would add the workbook and transfer the page
Next i
答案 0 :(得分:1)
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
你必须复制以避免弄乱计数,并且新工作簿有一个额外的工作表 - 但快速
for each wb in workbooks:wb.sheets("sheet1").delete:next wb
将解决该问题