|
三词典算法,目前来看应该是速度最快的:
- Sub test3()
- t = Timer
- Dim iArr_Data(), iArr_BOM(), iArr_Total(), i&, j&, n&, xms, d, mkey ', iArr_Group()
- Dim iDict_Total As Object
- ReDim iArr_Total(1 To 10000, 1 To 2)
- Set iDict_Total = CreateObject("Scripting.Dictionary")
- Set iDict_M = CreateObject("Scripting.Dictionary")
- Set d = CreateObject("Scripting.Dictionary")
-
- With Sh_Data
- .Range("H2:I65536").ClearContents
- iArr_BOM = .Range("A3:C12408")
- iArr_Data = .Range("E3:F" & .Range("E65526").End(xlUp).Row)
- For i = 1 To UBound(iArr_BOM)
- If Not iDict_Total.exists(iArr_BOM(i, 1)) Then
- iDict_Total(iArr_BOM(i, 1)) = i
- Else
- iDict_Total(iArr_BOM(i, 1)) = iDict_Total(iArr_BOM(i, 1)) & "++" & i
- End If
- Next
- For i = 1 To UBound(iArr_Data)
- If Not d.exists(iArr_Data(i, 1)) Then
- d(iArr_Data(i, 1)) = iArr_Data(i, 2)
- Else
- d(iArr_Data(i, 1)) = d(iArr_Data(i, 1)) + iArr_Data(i, 2)
- End If
- Next
-
-
- n = 0
- For Each mkey In d.keys
- If iDict_Total.exists(mkey) Then
- xms = Split(iDict_Total(mkey), "++")
- For j = 0 To UBound(xms)
- If Not iDict_M.exists(iArr_BOM(xms(j), 2)) Then
- n = n + 1
- iDict_M(iArr_BOM(xms(j), 2)) = n
- iArr_Total(n, 1) = iArr_BOM(xms(j), 2)
- iArr_Total(n, 2) = iArr_BOM(xms(j), 3) * d(mkey)
- Else
- iArr_Total(iDict_M(iArr_BOM(xms(j), 2)), 2) = iArr_Total(iDict_M(iArr_BOM(xms(j), 2)), 2) + iArr_BOM(xms(j), 3) * d(mkey)
- End If
- Next
- End If
- Next
- .Range("M3").Resize(n, 2) = iArr_Total
- .Range("M3").Resize(n, 2).Sort Key1:=.Range("M3"), Order1:=xlAscending, Header:=xlNo
- End With
- MsgBox Timer - t
- End Sub
复制代码 |
|