Skip to main content

如何在 Excel 的列表中找到兩個數字的所有可能和?

Author: Sun Last Modified: 2025-05-12

如果您需要在列表中找到兩個數字的所有可能和,或者列出 Excel 中兩個列表的所有可能組合,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

提示:在程式碼字串 Range("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 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!