Skip to main content

Kutools for Office — 一套工具,五種功能。完成更多工作。

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

Author Sun Last modified

如果您需要在列表中找到兩個數字的所有可能和,或者列出 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功能!立即下載!

最佳 Office 生產力工具

🤖 Kutools AI 助手:以智能執行為基礎,革新數據分析 生成程式碼 創建自訂公式 分析數據並生成圖表 調用 Kutools 增強函數
熱門功能查找、選取項目的背景色或標記重複值刪除空行合併列或單元格且不遺失數據四捨五入(免公式)...
高級 LOOKUP多條件 VLookup多值 VLookup多表查找模糊查找...
高級下拉列表快速創建下拉列表 依賴型下拉列表 多選下拉列表...
列管理器添加指定數量的列移動列切換隱藏列的顯示狀態比較區域及列...
精選功能網格聚焦 設計檢視 增強編輯欄 工作簿及工作表管理器 資源庫(快捷文本) 日期提取器 合併資料 加密/解密儲存格 按列表發送電子郵件 超級篩選 特殊篩選(篩選粗體/傾斜/刪除線...)...
15 大工具集12 項文本工具添加文本刪除特定字符…)50+ 儀表 類型甘特圖等)40+ 實用 公式基於生日計算年齡等)19 項插入工具插入QR码根據路徑插入圖片等)12 項轉換工具金額轉大寫匯率轉換等)7 項合併與分割工具高級合併行分割儲存格等)...及更多
使用 Kutools,語言任你選 — 支援英語、西班牙語、德語、法語、中文及超過40 種語言!

運用 Kutools for Excel,全面提升您的 Excel 技能,體驗前所未有的高效。 Kutools for Excel 提供超過300 項進階功能,讓您提升工作效率、節省時間。 點此尋找您最需要的功能...


Office Tab 為 Office 帶來分頁介面,讓您的工作更加輕鬆簡單

  • 在 Word、Excel、PowerPoint 中啟用分頁編輯與閱讀
  • 在同一視窗的新分頁中打開與創建多份文件,而非開啟新視窗。
  • 提升您的生產力50%,每日可幫您減少數百次鼠標點擊!

所有 Kutools 外掛,一次安裝

Kutools for Office 套裝整合了 Excel、Word、Outlook 和 PowerPoint 的外掛,外加 Office Tab Pro,非常適合需要跨 Office 應用程式協同作業的團隊。

Excel Word Outlook Tabs PowerPoint
  • 全合一套裝 — Excel、Word、Outlook及 PowerPoint 外掛 + Office Tab Pro
  • 一鍵安裝,一份授權 — 幾分鐘完成設置(支援 MSI)
  • 協同運作更順暢 — Office 應用間無縫提升生產力
  • 30 天全功能試用 — 無需註冊、無需信用卡
  • 最超值 — 一次購買,節省單獨外掛費用