跳到主要内容

如何在Excel中的列表中找到两个数字的所有可能和?

假设这里是一个数字列表,现在您可以在此列表中找到两个数字的所有可能的总和,如下面的屏幕截图所示? 在Excel中,没有内置函数可以解决此任务。 现在,我向您介绍一个VBA代码来处理它。
doc两个数字的所有可能和1

Find all possible sums of two numbers in a list with VBA code

Find all possible combinations of two lists with List All Combinationsgood idea3


Find all possible sums of two numbers in a list with VBA code

如果要在列表中查找两个数字的所有可能和,可以在VBA代码下面应用。

1。 按 Alt + F11 键显示 Microsoft Visual Basic for Applications 窗口。

2。 点击 Insert > Module,将以下代码复制并粘贴到模块中。

VBA: List all possible sums of two numbers in a list

Sub Combinations()
'UpdatebyExtendoffice20180628
    Dim xRgArr
    Dim xNum As Long
    Dim I, J, K As Long
    Dim xTemp As Double
    Dim xRg As Range
    Dim xRgCount As Long
    Dim xDic As New Dictionary
    On Error Resume Next
    Set xRg = Application.InputBox("Select a list (one column):", "KuTools for Excel", Selection.Address, , , , , 8)
    If (xRg Is Nothing) Or (xRg.Count = 1) Then Exit Sub
    xRgCount = xRg.Count
    K = 1
    ReDim xRgArr(1 To xRgCount)
    For Each xCell In xRg
      xRgArr(K) = xCell.Value
      K = K + 1
    Next
    K = 0
    For I = 1 To xRgCount
        For J = I + 1 To xRgCount
            xTemp = xRgArr(I) + xRgArr(J)
            If Not xDic.Exists(xTemp) Then
                xDic.Add CDbl(xTemp), CStr(xTemp)
                K = K + 1
            End If
        Next
    Next
    Range("C1").Resize(xDic.Count, 1) = Application.WorksheetFunction.Transpose(xDic.Keys)
End Sub

doc两个数字的所有可能和2

3。 点击 Tools > References,在弹出的对话框中,选中 Microsoft Scripting Runtime 复选框。

doc两个数字的所有可能和3 doc箭头向右 doc两个数字的所有可能和4

4。 点击 OK 关闭对话框,然后按 F5 要运行代码,将弹出一个对话框,提醒您选择一个列表以查找可能的金额。
doc两个数字的所有可能和5

5。 点击 OK,则列表C中列出了列表中两个数字的所有可能总和。
doc两个数字的所有可能和6

Tip:在代码字符串Range(“ C1”)。Resize(xDic.Count,1)中,您可以根据需要将C1更改为另一个单元格。


Find all possible combinations of two lists with List All Combinations

如果要查找或列出两个列表的所有可能组合,如下面的屏幕截图所示,则 List All Combinations 实用程序 Kutools for Excel 可以帮你一个忙。
doc两个数字的所有可能和7

Kutools for Excel, 与超过 300 方便的功能,使您的工作更加轻松。 

1。 点击 Kutools > Insert > List All Combinations.
doc两个数字的所有可能和8

2.在 List All Combinations 对话框中,单击选择按钮以选择要合并的第一个列表。
doc两个数字的所有可能和9

3。 点击 Add 将列表添加到 Combinations list 部分。
doc两个数字的所有可能和10

4.重复步骤2和3,将第二个列表添加到 Combinations list.
doc两个数字的所有可能和11

5。 然后点击 Ok 选择要放置组合结果的单元格。
doc两个数字的所有可能和12

6。 点击 OK。 所有可能的组合已在单元格中列出。
doc两个数字的所有可能和13

Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations