跳至主要内容

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

Author: Sun Last Modified: 2025-05-07

如果需要在列表中找到两个数字的所有可能和,或者列出两个列表的所有可能组合,Excel并没有直接实现此功能的内置函数。本指南介绍了一种使用VBA代码计算和的方法,以及Kutools for Excel中的“列出所有组合”工具来高效处理组合问题的解决方案。
A screenshot showing a list of numbers for calculating all possible sums in Excel

使用VBA代码查找列表中两个数字的所有可能和

使用“列出所有组合”工具查找两个列表的所有可能组合good idea3


使用VBA代码查找列表中两个数字的所有可能和

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

1. 按 Alt + F11 键打开 Microsoft Visual Basic for Applications 窗口。

2. 单击 插入 > 模块,将以下代码复制并粘贴到模块中。

VBA:列出列表中两个数字的所有可能和

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

A screenshot of the VBA editor showing code for listing all possible sums of two numbers in Excel

3. 单击 工具 > 引用,在弹出的对话框中勾选 Microsoft Scripting Runtime 复选框。

A screenshot showing the References option in the VBA window Arrow right A screenshot showing the References dialog in Excel VBA for enabling the Microsoft Scripting Runtime library

4. 单击 确定 关闭对话框,然后按 F5 运行代码。将出现一个对话框,提示您选择一个列表以计算可能的和。
A screenshot showing the dialog to select a list for calculating all possible sums

5. 单击 确定,然后列表中两个数字的所有可能和将显示在C列中。
A screenshot showing all possible sums of two numbers listed in column C

提示:在代码字符串 区域("C1").Resize(xDic.Count, 1) 中,您可以将 C1 更改为任何其他想要放置结果的单元格。


使用“列出所有组合”工具查找两个列表的所有可能组合

如果您想列出两个列表的所有可能组合,如下图所示, 列出所有 组合 工具来自 Kutools for Excel 可以帮助您完成任务。
A screenshot showing two lists of values for creating all possible combinations in Excel

Kutools for Excel 提供了超过 300 种高级功能,简化复杂任务,提升创造力与效率。 通过集成 AI 能力,Kutools 能够精准自动执行任务,让数据管理变得轻松简单。Kutools for Excel 的详细信息...         免费试用...

1. 单击 Kutools > 插入 > 列出所有组合.
A screenshot showing the List All Combinations tool in Kutools tab in Excel

2. 在 列出所有组合 对话框中,单击选择按钮以选择要组合的第一个列表。
A screenshot showing the selection of the first list in the List All Combinations tool

3. 单击 添加 将列表添加到 组合列表 部分。
A screenshot showing how to add the first list to the combinations list in Kutools

4. 重复步骤2和3,将第二个列表添加到 组合列表.
A screenshot showing how to add the second list to the combinations list in Kutools

5. 然后单击 确定 选择一个单元格以放置组合结果。
A screenshot showing the cell selection dialog for placing combinations

6. 单击 确定。所有可能的组合都已列在单元格中。
A screenshot showing all possible combinations of two lists created in Excel using Kutools

Kutools for Excel - 通过超过300个必备工具,让Excel功能大幅提升。永久免费享受AI功能!立即获取

演示:使用Kutools for Excel查找两个列表的所有可能组合

Kutools for Excel:超过 300 种实用工具触手可及!永久免费享受 AI 功能!立即下载!

最佳办公效率工具

🤖 Kutools AI 助手:基于智能执行生成代码创建自定义公式分析数据并生成图表调用 Kutools 函数等功能,彻底改变数据分析方式…
热门功能查找、高亮或标记重复项 | 删除空行 | 合并不丢失数据的列或单元格 | 四舍五入 ...
高级 LOOKUP多条件 VLookup | 多值 VLookup | 多表查找 | 模糊查找 ....
高级下拉列表快速创建下拉列表 | 从属下拉列表 | 多选下拉列表 ....
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域和列 ...
精选功能网格聚焦 | 设计视图 | 增强编辑栏 | 工作簿与工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/斜体/删除线...)...
排名前 15 的工具集12 种文本 工具添加文本删除特定字符等) | 50 多 种图表 类型甘特图等) | 40 多种实用 公式基于生日计算年龄等) | 19 种插入 工具插入二维码根据路径插入图片等) | 12 种转换 工具小写金额转大写汇率转换等) | 7 种合并与分割 工具高级合并行分割单元格等) | 还有更多...

使用 Kutools for Excel 提升您的 Excel 技能,体验前所未有的高效。 Kutools for Excel 提供超过 300 种高级功能来提高生产力并节省时间。 单击此处获取您最需要的功能...


Office Tab 将标签式界面引入 Office,让您的工作更加轻松

  • 在 Word、Excel、PowerPoint 中启用标签式编辑和阅读
  • 在同一窗口的新标签页中打开和创建多个文档,而不是在新窗口中进行操作。
  • 将您的生产力提升 50%,每天为您减少数百次鼠标点击!