|
こんにちは
Sub test()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim wsh As Worksheet
Dim r As Range
Dim s As Range
Dim i As Long
Dim j As Long
Const m As Long = 3 '規定数
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
Set wsh = Worksheets.Add
Application.ScreenUpdating = False
sh2.UsedRange.Offset(1).ClearContents
wsh.Range("A1:C1").Value = sh1.Range("A1:C1").Value
wsh.Range("D1").Value = "グループ"
i = 2
For Each r In sh1.Range("A2", sh1.Range("A2").End(xlDown))
wsh.Cells(i, 1).Resize(r(1, 3), 3).Value = r.Resize(, 3).Value
i = i + r(1, 3)
Next
j = wsh.Range("A" & Rows.Count).End(xlUp).Row
With wsh.Range("D2:D" & j)
.Formula = "=B2&INT((ROW()+" & m - 2 & ")/" & m & ")"
.Value = .Value
End With
wsh.Range("A1").CurrentRegion.Subtotal _
GroupBy:=4, Function:=xlCount, TotalList:=Array(3), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Set s = wsh.Range("D2", wsh.Range("D2").End(xlDown).Offset(-1, 0)) _
.Offset(, -3).SpecialCells(xlCellTypeBlanks)
For Each r In s
r.Offset(-1, 0).Resize(, 2).Copy _
sh2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
sh2.Range("C" & Rows.Count).End(xlUp).Offset(1, 0).Value = _
r.Offset(0, 2).Value
Next
With sh2.Range("C2", sh2.Range("C2").End(xlDown)).Offset(0, 1)
.Formula = "=IF(D1=" & m & ",C2,D1+C2)"
.Value = .Value
End With
Application.DisplayAlerts = False
wsh.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
|
|