试图找到但未成功的东西
我需要的是创建一个电子表格,我将输入几个不同的数字。
我希望它们之间有所有可能的总和组合。
For example 1,2,3,4.....
1+2
1+3
1+4
2+3
2+4
3+4
.....
如果我也能知道组合来自哪两个数字,那将会很棒。
我希望很清楚。
提前谢谢!
答案 0 :(得分:6)
如果A2:A18有17个数字,
B1(航向):
=TRANSPOSE(A2:A18)
B2:
=ARRAYFORMULA(A2:A18+TRANSPOSE(A2:A18))
这将给出一个17 * 17的所有不同组合的SUM表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
答案 1 :(得分:2)
如果您只需要值对,那么请使用这个更简单的宏:
Sub PairsOnly()
Dim Items(1 To 17) As Variant
Dim i As Long, j As Long, k As Long
Dim lower As Long, upper As Long
lower = LBound(Items)
upper = UBound(Items)
k = 2
For i = lower To upper
Items(i) = Cells(1, i)
Next i
For i = lower To upper - 1
For j = i + 1 To upper
Cells(k, 1) = Items(i) & "," & Items(j)
Cells(k, 2) = Items(i) + Items(j)
k = k + 1
Next j
Next i
End Sub
答案 2 :(得分:1)
这适用于使用 VBA
的Excel在 A1 到 Q1 中列出您的17个值。然后运行这个宏:
Option Explicit
Sub ListSubsets()
Dim Items(1 To 17) As Variant
Dim CodeVector() As Integer
Dim i As Long, kk As Long
Dim lower As Long, upper As Long
Dim NewSub As String
Dim done As Boolean
Dim OddStep As Boolean
kk = 3
OddStep = True
lower = LBound(Items)
upper = UBound(Items)
For i = lower To upper
Items(i) = Cells(1, i)
Next i
ReDim CodeVector(lower To upper) 'it starts all 0
Application.ScreenUpdating = False
Do Until done
'Add a new subset according to current contents
'of CodeVector
NewSub = ""
For i = lower To upper
If CodeVector(i) = 1 Then
If NewSub = "" Then
NewSub = "'=" & Items(i)
Else
NewSub = NewSub & "+" & Items(i)
End If
End If
Next i
If NewSub = "" Then NewSub = "{}" 'empty set
Cells(kk, 2) = NewSub
Cells(kk, 3).Formula = Mid(NewSub, 2)
kk = kk + 1
'now update code vector
If OddStep Then
'just flip first bit
CodeVector(lower) = 1 - CodeVector(lower)
Else
'first locate first 1
i = lower
Do While CodeVector(i) <> 1
i = i + 1
Loop
'done if i = upper:
If i = upper Then
done = True
Else
'if not done then flip the *next* bit:
i = i + 1
CodeVector(i) = 1 - CodeVector(i)
End If
End If
OddStep = Not OddStep 'toggles between even and odd steps
Loop
Application.ScreenUpdating = True
End Sub
组合将从 B4 向下显示,相关的总和在 D 列中显示:
注:
在旧笔记本电脑上运行大约需要4分钟。