|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Option Explicit
Sub TEST2()
Dim ar, br, cr, dr, i&, j&, vKey, dic(1) As New Dictionary
Application.ScreenUpdating = False
ar = Range("A2", Cells(Rows.Count, "C").End(xlUp)).Value
br = Range("F2", Cells(Rows.Count, "L").End(xlUp)).Value
ReDim Preserve ar(1 To UBound(ar), UBound(ar, 2))
For j = 3 To 2 Step -1
For i = 1 To UBound(ar)
ar(i, j) = ar(i, j - 1)
Next i
Next j
ar(1, 1) = "仓库名": ar(1, 3) = "调拔量"
For i = 2 To UBound(ar)
dic(0)(br(i, 1)) = dic(0)(br(i, 1)) & " " & i
Next i
For Each vKey In dic(0).keys
cr = Split(dic(0)(vKey))
For j = 4 To UBound(br, 2)
ReDim dr(1 To UBound(cr), 1 To 3)
For i = 1 To UBound(cr)
dr(i, 1) = br(cr(i), 2)
dr(i, 2) = br(cr(i), 3)
dr(i, 3) = br(cr(i), j)
Next i
ShellSort2D dr, 1, UBound(dr), 1, UBound(dr, 2), 3
dic(1)(vKey & Right(br(1, j), 1)) = dr
Next j
Next
For i = 2 To UBound(ar)
br = dic(1)(ar(i, 0) & ar(i, 1))
For j = 1 To UBound(br)
If br(j, 2) >= ar(i, 3) Then ar(i, 1) = br(j, 1): Exit For
Next j
Next i
[P1].Resize(UBound(ar), UBound(ar, 2) + 1) = ar
Application.ScreenUpdating = True
Beep
End Sub
Function ShellSort2D(ByRef ar, ByVal iFirst&, ByVal iLast&, ByVal iLeft&, _
ByVal iRight&, ByVal iKey&, Optional isOrder As Boolean = True)
Dim iRowSize&, vTemp, interval&, i&, j&, k&
ReDim vTemp(iLeft To iRight)
iRowSize = iLast - iFirst + 1
interval = 1
If iRowSize > 13 Then
Do While interval < iRowSize
interval = interval * 3 + 1
Loop
interval = interval \ 9
End If
Do While interval
For i = iFirst + interval To iLast
For j = iLeft To iRight
vTemp(j) = ar(i, j)
Next
If isOrder Then
For k = i - interval To iFirst Step -interval
If ar(k, iKey) <= vTemp(iKey) Then Exit For
For j = iLeft To iRight
ar(k + interval, j) = ar(k, j)
Next j
Next k
Else
For k = i - interval To iFirst Step -interval
If ar(k, iKey) > vTemp(iKey) Then Exit For
For j = iLeft To iRight
ar(k + interval, j) = ar(k, j)
Next j
Next k
End If
For j = iLeft To iRight
ar(k + interval, j) = vTemp(j)
Next
Next i
interval = interval \ 3
Loop
End Function
|
评分
-
1
查看全部评分
-
|