こんにちは
新規ブックを作成する時に、シートは2枚以上出来ていますでしょうか?
Sub test()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim sh3 As Worksheet
Dim sh4 As Worksheet
Dim tBK As Workbook
Dim r As Range
Dim t As Range
Dim i As Long
Dim j As Long
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
Set sh3 = Worksheets("Sheet3")
Set sh4 = Worksheets("Sheet4")
Application.ScreenUpdating = False
j = Application.SheetsInNewWorkbook
Application.SheetsInNewWorkbook = 2
With sh4
Set t = .Range("A1").CurrentRegion.Columns(1).Cells
For Each r In t
If r.Row > 1 Then
sh1.Range("B2:B7").Value = _
Application.Transpose(r.Resize(, 6).Value)
Set tBK = Workbooks.Add
sh2.Cells.Copy
tBK.Worksheets(1).Range("A1").PasteSpecial xlValues
sh3.Cells.Copy
tBK.Worksheets(2).Range("A1").PasteSpecial xlValues
tBK.SaveAs ThisWorkbook.Path & "\" & i & Format(Now(), "yyyymmdd hhmmss") & ".xlsx"
tBK.Close
i = i + 1
End If
DoEvents
Next
End With
Application.SheetsInNewWorkbook = j
Application.ScreenUpdating = True
End Sub
とすると、どうですか?
こんにちは
Sheet2、Sheet3には数式がセットされているのですよね?
とすれば、Sheet1のB2~B7へSheet4のパターン区分をセットして
Sheet2、Sheet3を別ブックとして保存するだけでいいのですよね?
Sub test()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim sh3 As Worksheet
Dim sh4 As Worksheet
Dim tBK As Workbook
Dim r As Range
Dim t As Range
Dim i As Long
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
Set sh3 = Worksheets("Sheet3")
Set sh4 = Worksheets("Sheet4")
Application.ScreenUpdating = False
With sh4
Set t = .Range("A1").CurrentRegion.Columns(1).Cells
For Each r In t
If r.Row > 1 Then
sh1.Range("B2:B7").Value = _
Application.Transpose(r.Resize(, 6).Value)
Set tBK = Workbooks.Add
sh2.Cells.Copy
tBK.Worksheets(1).Range("A1").PasteSpecial xlValues
sh3.Cells.Copy
tBK.Worksheets(2).Range("A1").PasteSpecial xlValues
tBK.SaveAs ThisWorkbook.Path & "\" & i & Format(Now(), "yyyymmdd hhmmss") & ".xlsx"
tBK.Close
i = i + 1
End If
Next
End With
Application.ScreenUpdating = True
End Sub
1万パターンも有ったら相当時間掛かると思います。
お礼
できています!ありがとうございます!! j = Application.SheetsInNewWorkbook Application.SheetsInNewWorkbook = 2 の2行で こんなにも快適に動作するんですね!? なるほど!シート数を先に指定してしまうんですか・・・ 勉強になります!! いつも本当にありがとうございます!!!!