如何在Excel中找到所有数字组合,它们的和在一个范围内?

huangapple go评论137阅读模式
英文:

How to find all combinations of numbers that have a sum in a range in Excel?

问题

I can help you with the translation:

如果我有一组数字,例如:

列 A
14
80
27
16
85
22
79
26

如何在Excel中找到所有数字组合,它们的和在一个范围内?

如何找到所有数字组合,使它们的和在范围内(120 到 160)?

例如:

结果将给出在范围内的所有数字组合:
(80,79),(16,22,26,27,79),(26,27,79),...

我尝试使用 Kutools 来找到组合,但它只支持找到等于给定数字的所有组合(不支持范围)。

Kutools 指南

英文:

I have a question about Excel.

If I have a set of numbers, for example:

Column A
14
80
27
16
85
22
79
26

如何在Excel中找到所有数字组合,它们的和在一个范围内?
How can I find all combinations of numbers with a sum in a range (120 to 160) ????

For example:

The result will give me all combinations of all numbers in a combination in range (of 120 to 160):
(80,79) , (16,22,26,27,79), (26,27,79), ....

I tried to use Kutools to find the combinations, but it just supported finding all combinations that equal a given number (not in a range).

Kutools guide

答案1

得分: 0

I believe you need to brute force loop through all possible combinations, so be aware this can take a long time to run if you have a long list of numbers. It became noticeably slow for me if the list was around 20 numbers.

如果你需要通过蛮力循环遍历所有可能的组合,要注意如果你有一个很长的数字列表,这可能需要很长时间来运行。如果列表大约有20个数字,我注意到运行速度会明显变慢。

If you have n numbers in the list, there are 2(n-1) possible combinations of those numbers, and that scales very rapidly. For a list of 8 numbers, there are 255 combinations. For a list of 22 numbers, there are 4,194,303 combinations.

如果你的列表中有n个数字,那么这些数字的可能组合数为2(n-1),这个数量增长非常迅速。对于8个数字的列表,有255种组合。对于22个数字的列表,有4,194,303种组合。

In the macro, we loop through each of those combinations. We assign a power of 2 to each number in the list (1,2,4,8,16...), and this allows us to test whether each number belongs to each possible combination. We add up the total for each combination, check whether it falls within our desired range, and add it to a collection. At the end of the macro we loop through the collection and print it to the sheet.

在宏中,我们遍历每一个组合。我们为列表中的每个数字分配一个2的幂次方 (1,2,4,8,16...),这样我们可以测试每个数字是否属于每个可能的组合。我们计算每个组合的总和,检查它是否在我们期望的范围内,并将其添加到一个集合中。在宏的末尾,我们遍历集合并将其打印到工作表上。

英文:

I believe you need to brute force loop through all possible combinations, so be aware this can take a long time to run if you have a long list of numbers. It became noticeably slow for me if the list was around 20 numbers.

If you have n numbers in the list, there are 2(n-1) possible combinations of those numbers, and that scales very rapidly. For a list of 8 numbers, there are 255 combinations. For a list of 22 numbers, there are 4,194,303 combinations.

In the macro, we loop through each of those combinations. We assign a power of 2 to each number in the list (1,2,4,8,16...), and this allows us to test whether each number belongs to each possible combination. We add up the total for each combination, check whether it falls within our desired range, and add it to a collection. At the end of the macro we loop through the collection and print it to the sheet.

Sub all_combinations()

Dim arrNumbers As Variant, arrTemp As Variant
Dim collOutput As Collection

Dim FirstNumber As Range, Output As Range
Dim LowLimit As Long, HiLimit As Long

Dim i As Long
Dim CurrentComb As Long, CombTest As Long, CombTotal As Long
Dim NumPower As Long
Dim arrIndex As Long

'Create collection for output
Set collOutput = New Collection

'Assign ranges
Set FirstNumber = Range("D2")   'First number in list
LowLimit = Range("G3")  'Lower limit
HiLimit = Range("H3")   'Upper limit
Set Output = Range("O5")    'First cell to write results

'Assign numbers to array
arrNumbers = Range(FirstNumber, FirstNumber.End(xlDown)).Value

'Resize temporary array
ReDim arrTemp(1 To UBound(arrNumbers))

'Loop through all possible combinations 1 to 2^(n-1)
For CurrentComb = 1 To 2 ^ (UBound(arrNumbers) - 1)
    
    ReDim arrTemp(1 To UBound(arrNumbers))  'Clear temporary array
    CombTest = CurrentComb  'Assign limit for power-of-2 numbers
    
    'Loop through number list
    For i = UBound(arrNumbers, 1) To 1 Step -1
        
        NumPower = 2 ^ (i - 1)  'Assign power-of-2 to each position in the number list
        
        'Check if power-of-2 number is less than current limit
        If NumPower <= CombTest Then
            arrIndex = arrIndex + 1 'move to next position in temp array
            arrTemp(arrIndex) = arrNumbers(i, 1)    'write current number to temp array
            CombTotal = CombTotal + arrNumbers(i, 1)   'Update current combination total
            CombTest = CombTest - NumPower  'Update current power-of-2 limit
        End If
    
    Next i
    
    'Check if current combination total is within low / high limit
    If CombTotal >= LowLimit And CombTotal <= HiLimit Then
        collOutput.Add arrTemp  'Add temp array to output collection
    End If
    
    'clear combination total, reset temp array index
    CombTotal = 0
    arrIndex = 0

Next CurrentComb

'Write to sheet
Dim item As Variant, itemPart As Variant
Dim CurrentRow As Long, CurrentCol As Long

CurrentRow = Output.Row

'Loop through each item in collection
For Each item In collOutput
    CurrentCol = Output.Column
    'Loop through each item in each array
    For Each itemPart In item
        Cells(CurrentRow, CurrentCol).Value = itemPart
        CurrentCol = CurrentCol + 1
    Next itemPart
    CurrentRow = CurrentRow + 1
Next item

End Sub

答案2

得分: 0

这可以通过添加 Solver 插件来实现...将是 4 行代码

Solver 重置
Solverok
Solveradd
solvertest

观看此视频

如果您需要我根据您的问题编写代码,请附上带有虚拟数据或数据示例的文件。

英文:

this can be achieved with Solver Add-in...shall be 4 lines of code

Solver reset
Solverok
Solveradd
solvertest

Watch this

let me know in case you need me code wrt your problem, please attach the file with dummy data or data example

huangapple
  • 本文由 发表于 2023年5月6日 23:53:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定