|
▼jun さん:
>マクロの記録だと範囲は限定されたり色々制限ありますので、
...んん...これを解析するよりピボットベース+ひと工夫。の方がラクな気はしますけど
Sub sample()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim dicA As Object
Dim dicB As Object
Dim dicC As Object
Dim i As Long
Dim j As Long
Dim n As Long
Dim vntA, vntB, x
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
Set dicA = CreateObject("Scripting.Dictionary")
Set dicB = CreateObject("Scripting.Dictionary")
Set dicC = CreateObject("Scripting.Dictionary")
sh2.UsedRange.Clear
vntA = sh1.Range("A2").CurrentRegion.Value
For i = 1 To UBound(vntA)
x = vntA(i, 1) & "," & vntA(i, 2) & vntA(i, 3)
dicA(x) = dicA(x) + vntA(i, 4)
dicB(vntA(i, 1) & "," & vntA(i, 2)) = Empty
dicC(vntA(i, 3)) = Empty
Next i
With sh2.Range("A1").Resize(dicB.Count)
.Value = Application.Transpose(dicB.keys())
.TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
Comma:=True, _
FieldInfo:=Array(Array(1, 2), Array(2, 2))
End With
With sh2.Range("B1")
.Resize(, dicC.Count).Value = dicC.keys()
.Value = sh1.Range("B2").Value
End With
With sh2.Range("A1").CurrentRegion
vntB = .Resize(, .Columns.Count + 1).Value
n = UBound(vntB, 2)
vntB(1, n) = "不良数計"
For i = 2 To UBound(vntB)
For j = 3 To dicC.Count + 1
vntB(i, j) = dicA(vntB(i, 1) & "," & vntB(i, 2) & vntB(1, j))
vntB(i, n) = vntB(i, n) + vntB(i, j)
Next
Next i
.Resize(, .Columns.Count + 1).Value = vntB
End With
Set sh1 = Nothing
Set sh2 = Nothing
Set dicA = Nothing
Set dicB = Nothing
Set dicC = Nothing
End Sub
ちょっと手抜きしてます。&眠いので今日はここまでネm(_ _)m
|
|