如何在 Excel 中找到所有等於給定總和的組合?
發現列表中加起來等於特定總和的所有可能數字組合,是許多 Excel 使用者可能會遇到的挑戰,無論是為了預算、規劃還是數據分析的目的。
在此範例中,我們有一個數字列表,目標是找出列表中哪些組合的總和為 480。提供的截圖顯示有五組可能的組合達到此總和,包括像 300+120+60、250+120+60+50 等組合。在本文中,我們將探討各種方法來確定列表中總和為指定值的特定數字組合。
使用求解器函數找到等於給定總和的單元格組合
深入研究 Excel 找到加起來等於特定數字的單元格組合可能看似困難,但求解器外掛程式讓這變得輕而易舉。我們將引導您完成簡單的步驟來設置求解器並找到正確的單元格組合,讓看似複雜的任務變得簡單且可行。
步驟 1:啟用求解器外掛程式
- 請前往 File > Options,在 Excel Options 對話框中,從左側窗格點擊 Add-Ins,然後點擊 Go 按鈕。參見截圖:
- 接著,出現 Add-Ins 對話框,勾選 Solver Add-in 選項,並點擊 OK 安裝該外掛程式。
步驟 2:輸入公式
啟動求解器外掛程式後,您需要在 B11 單元格中輸入以下公式:
=SUMPRODUCT(B2:B10,A2:A10)
步驟 3:配置並運行求解器以獲得結果
- 點擊 Data > Solver 進入 Solver Parameter 對話框,在對話框中,請執行以下操作:
- (1.) 點擊
按鈕選擇單元格 B11 您的公式所在的位置,位於 Set Objective 部分;
- (2.) 然後在 To 部分,選擇 Value Of,並根據需要輸入您的目標值 480;
- (3.) 在 By Changing Variable Cells 部分,請點擊
按鈕選擇單元格範圍 B2:B10 將標記出相應的數字。
- (4.) 接著,點擊 Add 按鈕。
- (1.) 點擊
- 然後,出現 Add Constraint 對話框,點擊
按鈕選擇單元格範圍 B2:B10,並選擇 bin 從下拉列表中。最後,點擊 OK 按鈕。參見截圖:
- 在 Solver Parameter 對話框中,點擊 Solve 按鈕,幾分鐘後,會彈出 Solver Results 對話框,您可以看到等於給定總和 480 的單元格組合在 B 列中被標記為 1。在 Solver Results 對話框中,請選擇 Keep Solver Solution 選項,並點擊 OK 離開對話框。參見截圖:
使用用戶自定義函數獲取所有等於給定總和的數字組合
為了揭示來自特定集合的所有可能數字組合,這些組合共同達到給定值,下面概述的自定義函數作為一種有效的工具。
步驟 1:打開 VBA 模塊編輯器並複製代碼
- 在 Excel 中按住 ALT + F11 鍵,打開 Microsoft Visual Basic for Applications 窗口。
- 點擊 Insert > Module,並將以下代碼粘貼到模塊窗口中。
VBA 代碼:獲取所有等於給定總和的數字組合Public Function MakeupANumber(xNumbers As Range, xCount As Long) 'updateby Extendoffice Dim arrNumbers() As Long Dim arrRes() As String Dim ArrTemp() As Long Dim xIndex As Long Dim rg As Range MakeupANumber = "" If xNumbers.CountLarge = 0 Then Exit Function ReDim arrNumbers(xNumbers.CountLarge - 1) xIndex = 0 For Each rg In xNumbers If IsNumeric(rg.Value) Then arrNumbers(xIndex) = CLng(rg.Value) xIndex = xIndex + 1 End If Next rg If xIndex = 0 Then Exit Function ReDim Preserve arrNumbers(0 To xIndex - 1) ReDim arrRes(0) Call Combinations(arrNumbers, xCount, ArrTemp(), arrRes()) ReDim Preserve arrRes(0 To UBound(arrRes) - 1) MakeupANumber = arrRes End Function Private Sub Combinations(Numbers() As Long, Count As Long, ArrTemp() As Long, ByRef arrRes() As String) Dim currentSum As Long, i As Long, j As Long, k As Long, num As Long, indRes As Long Dim remainingNumbers() As Long, newCombination() As Long currentSum = 0 If (Not Not ArrTemp) <> 0 Then For i = LBound(ArrTemp) To UBound(ArrTemp) currentSum = currentSum + ArrTemp(i) Next i End If If currentSum = Count Then indRes = UBound(arrRes) ReDim Preserve arrRes(0 To indRes + 1) arrRes(indRes) = ArrTemp(0) For i = LBound(ArrTemp) + 1 To UBound(ArrTemp) arrRes(indRes) = arrRes(indRes) & "," & ArrTemp(i) Next i End If If currentSum > Count Then Exit Sub If (Not Not Numbers) = 0 Then Exit Sub For i = 0 To UBound(Numbers) Erase remainingNumbers() num = Numbers(i) For j = i + 1 To UBound(Numbers) If (Not Not remainingNumbers) <> 0 Then ReDim Preserve remainingNumbers(0 To UBound(remainingNumbers) + 1) Else ReDim Preserve remainingNumbers(0 To 0) End If remainingNumbers(UBound(remainingNumbers)) = Numbers(j) Next j Erase newCombination() If (Not Not ArrTemp) <> 0 Then For k = 0 To UBound(ArrTemp) If (Not Not newCombination) <> 0 Then ReDim Preserve newCombination(0 To UBound(newCombination) + 1) Else ReDim Preserve newCombination(0 To 0) End If newCombination(UBound(newCombination)) = ArrTemp(k) Next k End If If (Not Not newCombination) <> 0 Then ReDim Preserve newCombination(0 To UBound(newCombination) + 1) Else ReDim Preserve newCombination(0 To 0) End If newCombination(UBound(newCombination)) = num Combinations remainingNumbers, Count, newCombination, arrRes Next i End Sub
步驟 2:輸入自定義公式以獲取結果
粘貼代碼後,關閉代碼窗口返回工作表。在空白單元格中輸入以下公式以輸出結果,然後按 Enter 鍵獲取所有組合。參見截圖:
=MakeupANumber(A2:A10,B2)
=TRANSPOSE(MakeupANumber(A2:A10,B2))

- 此自定義函數僅適用於 Excel 365 和 2021。
- 此方法僅適用於正數;小數值會自動四捨五入到最接近的整數,負數將導致錯誤。
使用強大功能獲取所有等於給定總和的數字組合
鑑於上述函數的局限性,我們推薦一種快速且全面的解決方案:Kutools for Excel 的 Make up a Number 功能,該功能兼容任何版本的 Excel。這個替代方案可以有效地處理正數、小數和負數。使用此功能,您可以快速獲取所有等於給定總和的組合。
- 點擊 Kutools > Content > Make Up A Number,參見截圖:
- 然後,在 Make up a number 對話框中,請點擊
按鈕選擇您想使用的數字列表,來自 Data Source,然後在 Sum 文本框中輸入總數。最後,點擊 OK 按鈕,參見截圖:
- 然後,會彈出一個提示框提醒您選擇一個單元格來放置結果,然後點擊 OK,參見截圖:
- 現在,所有等於給定數字的組合都已顯示如下截圖所示:
使用 VBA 代碼獲取總和在某範圍內的所有數字組合
有時,您可能會遇到需要識別所有可能的數字組合的情況,這些數字的總和落在特定範圍內。例如,您可能希望找到每一個可能的數字組合,其中總和介於 470 和 480 之間。
發現總和落在特定範圍內的所有可能數字組合代表了 Excel 中一個有趣且非常實用的挑戰。本節將介紹一種解決此任務的 VBA 代碼。
步驟 1:打開 VBA 模塊編輯器並複製代碼
- 在 Excel 中按住 ALT + F11 鍵,打開 Microsoft Visual Basic for Applications 窗口。
- 點擊 Insert > Module,並將以下代碼粘貼到模塊窗口中。
VBA 代碼:獲取總和在特定範圍內的所有數字組合Sub Getall_combinations() 'Updateby Extendoffice Dim xNumbers As Variant Dim Output As Collection Dim rngSelection As Range Dim OutputCell As Range Dim LowLimit As Long, HiLimit As Long Dim i As Long, j As Long Dim TotalCombinations As Long Dim CombTotal As Double Set Output = New Collection On Error Resume Next Set rngSelection = Application.InputBox("Select the range of numbers:", "Kutools for Excel", Type:=8) If rngSelection Is Nothing Then MsgBox "No range selected. Exiting macro.", vbInformation, "Kutools for Excel" Exit Sub End If On Error GoTo 0 xNumbers = rngSelection.Value LowLimit = Application.InputBox("Select or enter the low limit number:", "Kutools for Excel", Type:=1) HiLimit = Application.InputBox("Select or enter the high limit number:", "Kutools for Excel", Type:=1) On Error Resume Next Set OutputCell = Application.InputBox("Select the first cell for output:", "Kutools for Excel", Type:=8) If OutputCell Is Nothing Then MsgBox "No output cell selected. Exiting macro.", vbInformation, "Kutools for Excel" Exit Sub End If On Error GoTo 0 TotalCombinations = 2 ^ (UBound(xNumbers, 1) * UBound(xNumbers, 2)) For i = 1 To TotalCombinations - 1 Dim tempArr() As Double ReDim tempArr(1 To UBound(xNumbers, 1) * UBound(xNumbers, 2)) CombTotal = 0 Dim k As Long: k = 0 For j = 1 To UBound(xNumbers, 1) If i And (2 ^ (j - 1)) Then k = k + 1 tempArr(k) = xNumbers(j, 1) CombTotal = CombTotal + xNumbers(j, 1) End If Next j If CombTotal >= LowLimit And CombTotal <= HiLimit Then ReDim Preserve tempArr(1 To k) Output.Add tempArr End If Next i Dim rowOffset As Long rowOffset = 0 Dim item As Variant For Each item In Output For j = 1 To UBound(item) OutputCell.Offset(rowOffset, j - 1).Value = item(j) Next j rowOffset = rowOffset + 1 Next item End Sub
步驟 2:執行代碼
- 粘貼代碼後,按 F5 鍵運行此代碼,在第一個彈出的對話框中,選擇您想使用的數字範圍,然後點擊 OK。參見截圖:
- 在第二個提示框中,選擇或輸入最低限值數字,然後點擊 OK。參見截圖:
- 在第三個提示框中,選擇或輸入最高限值數字,然後點擊 OK。參見截圖:
- 在最後一個提示框中,選擇一個輸出單元格,這是結果開始輸出的地方。然後點擊 OK。參見截圖:
結果
現在,每個符合條件的組合將在工作表中的連續行中列出,從您選擇的輸出單元格開始。
Excel 提供了幾種方法來找到加起來等於特定總數的數字組合,每種方法的工作方式不同,因此您可以根據自己對 Excel 的熟悉程度以及項目需求選擇一種。如果您對探索更多 Excel 技巧感興趣,我們的網站提供了數千個教程。感謝您的閱讀,我們期待未來為您提供更多有用的信息!
相關文章:
- 列出或生成所有可能的組合
- 假設,我有以下兩列數據,現在,我想根據這兩個值列表生成所有可能的組合,如左側截圖所示。也許,如果只有少數幾個值,您可以逐一列出所有組合,但如果有多列具有多個值需要列出所有可能的組合,這裡有一些快速技巧可能幫助您在 Excel 中解決這個問題。
- 列出單列中的所有可能組合
- 如果您想從單列數據中返回所有可能的組合以獲得如下截圖所示的結果,您是否有任何快速的方法來處理這個任務?
- 生成 3 或多列的所有組合
- 假設,我有 3 列數據,現在,我想生成或列出這些 3 列數據的所有組合,如下截圖所示。您是否有任何好的方法來解決這個任務?
- 生成所有可能的 4 位數字組合列表
- 在某些情況下,我們可能需要生成 0 到 9 的所有可能的 4 位數字組合列表,這意味著生成 0000、0001、0002…9999 的列表。為了快速解決 Excel 中的列表任務,我介紹一些技巧給您。
最佳辦公效率工具
🤖 | 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%,每天為您減少數百次鼠標點擊!