任意两两三三四四相加等于指定数的组合选择宏代码
Option Explicit
Dim arr1 As Variant, j%, z%
Dim dic As New Dictionary
Sub xxxx()
j = Range("a65536").End(xlUp).Row
arr1 = Range("a1:a" & j)
z = InputBox("请输入要得到的结果值:", "hello!!", "75")
zh 1, 0, ""
If dic.Count = 0 Then
MsgBox "无解!!!"
GoTo aa:
End If
Range("b1").EntireColumn.Clear
Range("b1:b" & dic.Count) = WorksheetFunction.Transpose(dic.Keys)
Range("b1").EntireColumn.AutoFit
MsgBox "共" & dic.Count & "种结果"
aa: Set dic = Nothing
End Sub
Sub zh(i%, x%, y$)
If x + arr1(i, 1) = z Then
If Not dic.Exists(y & arr1(i, 1) & "=" & z) Then
dic.Add y & arr1(i, 1) & "=" & z, ""
End If
End If
If i < j And x < z Then
If x + arr1(i, 1) < z Then zh i + 1, x + arr1(i, 1), y & arr1(i, 1) & "+"
zh i + 1, x, y
End If