|
Option Explicit
Sub TEST6()
Dim ar, br, vResult, i&, j&, r&, dic As Object, vKey
Application.ScreenUpdating = False
Set dic = CreateObject("Scripting.Dictionary")
ReDim vResult(1 To 10 ^ 5, 1 To 3)
ar = Range("B4", Cells(Rows.Count, "C").End(xlUp)).Value
For i = 1 To UBound(ar)
dic(ar(i, 1)) = dic(ar(i, 1)) & " " & ar(i, 2)
Next i
ar = Split("城市 金额排列组合 排列组合后金额求和")
r = 1
For i = 0 To UBound(ar)
vResult(r, i + 1) = ar(i)
Next i
For Each vKey In dic.keys
ar = Split(dic(vKey))
For j = UBound(ar) To 1 Step -1
br = combinArr1(ar, j)
For i = 1 To UBound(br)
r = r + 1
vResult(r, 1) = vKey
vResult(r, 2) = br(i)
vResult(r, 3) = Evaluate(br(i))
Next i
Next j
Next
With [J3].Resize(r, UBound(vResult, 2))
.HorizontalAlignment = xlCenter
.Borders.LineStyle = xlContinuous
.EntireColumn.AutoFit
.Value = vResult
End With
Set dic = Nothing
Application.ScreenUpdating = True
Beep
End Sub
Function combinArr1(ByVal ar, ByVal n&)
Dim br&(), cr, vResult, i&, j&, m&, iCount&, iGroup&, vTemp
m = UBound(ar)
iGroup = Application.Combin(m, n)
ReDim vResult(1 To iGroup)
ReDim br&(1 To n)
ReDim cr(1 To n)
If n = 1 Then
For i = 1 To iGroup
cr(1) = ar(i)
vResult(i) = Join(cr, "+")
Next i
combinArr1 = vResult
Exit Function
End If
For j = 1 To n - 1: br(j) = j: Next
Do
For i = br(n - 1) + 1 To m
br(n) = i
iCount = iCount + 1
For j = 1 To n
cr(j) = ar(br(j))
Next j
vResult(iCount) = Join(cr, "+")
Next
If br(n - 1) < br(n) - 1 Then
br(n - 1) = br(n - 1) + 1
Else
For j = n - 2 To 1 Step -1
If br(j) <> br(j + 1) - 1 Then
vTemp = br(j) + 1: br(j) = vTemp: j = j + 1
Do Until j = n
br(j) = br(j - 1) + 1: j = j + 1
Loop
Exit For
End If
Next
End If
Loop Until iCount = iGroup
combinArr1 = vResult
End Function
|
评分
-
1
查看全部评分
-
|