このコードは
- ワークブックを一度に 3 シートのバッチの新しいワークブックに分割し、
- 以下の名前で新しいファイルとして保存します
- それらを閉じる
File1 (最初の 3 シート)
File4 (シート 4-6)
File7 (シート 7-9)
このコードは、Excel ファイルを余分なシートで「パディング」して、3 シート分割の複数を維持します。
を使用して新しいワークブックを作成できることに注意してください.Copy
-使用する必要はありませんWorkbooks.Add
Code to be run from the Workbook to be split
Sub BatchThree()
Dim lngSht As Long
Dim lngShtAdd As Long
Dim lngShts As Long
Dim bSht As Boolean
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
lngSht = 1
'pad extra sheets
If ThisWorkbook.Sheets.Count Mod 3 <> 0 Then
bSht = True
lngShts = ThisWorkbook.Sheets.Count Mod 3
For lngShtAdd = 3 To (lngShts + 1) Step -1
ThisWorkbook.Sheets.Add after:=ThisWorkbook.Sheets(Sheets.Count)
Next
End If
Do While lngSht + 2 <= ThisWorkbook.Sheets.Count
Sheets(Array(lngSht, lngSht + 1, lngSht + 2)).Copy
ActiveWorkbook.SaveAs ThisWorkbook.Path & "/File" & lngSht
ActiveWorkbook.Close False
lngSht = lngSht + 3
Loop
'remove extra sheets
If bSht Then
For lngShtAdd = 3 To (lngShts + 1) Step -1
ThisWorkbook.Sheets(Sheets.Count).Delete
Next
End If
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub