如何從 Excel 的單列中列出所有可能的組合?
如果您想從單列數據中返回所有可能的組合以獲得如下屏幕截圖所示的結果,您是否有任何快速的方法可以在 Excel 中處理此任務?
用公式列出單列中所有可能的組合
以下數組公式可以幫助您完成這項工作,請逐步執行:
1. 首先,您應該創建兩個輔助公式單元格。 在 C1 單元格中,請輸入以下公式,然後按 Ctrl + Shift + Enter 獲得結果的關鍵:
=MAX(LEN(A2:A6))
備註:在此公式中, A2:A6 是您要列出其組合的單元格列表。
2. 在單元格 C2 中,輸入以下公式,然後按 Ctrl + Shift + Enter 鍵一起得到第二個結果,見截圖:
=CONCAT(A2:A6&REPT(" ",C2-LEN(A2:A6)))
備註:在此公式中, A2:A6 是您要列出其組合的單元格列表, C2 單元格是否包含您在步驟 1 中創建的公式。
3. 然後,將以下公式複制並粘貼到單元格 D2 中,然後按 Ctrl + Shift + Enter 鍵在一起以獲得第一個結果,請參見屏幕截圖:
=IF(ROW()>2^(COUNTA(A$2:A$6)),"",TEXTJOIN(" + ",TRUE,IF(MID(TEXT(DEC2BIN(ROW()-1),REPT("0",COUNTA($A$2:$A$6))),ROW(INDIRECT("1:"&COUNTA($A$2:$A$6))),1)+0,TRIM(MID($C$3,(ROW(INDIRECT("1:"&COUNTA($A$2:$A$6)))-1)*$C$2+1,$C$2)),"")))
備註:在此公式中, A2:A6 是您要列出其組合的單元格列表, C2 單元格是否包含您在步驟 1 中創建的公式, C3 是具有您在步驟 2 中創建的公式的單元格, + 字符是分隔組合的分隔符,您可以根據需要更改它們。
4. 然後,選擇此公式單元格,然後向下拖動填充柄,直到出現空白單元格。 現在,您可以看到指定列數據的所有組合顯示如下演示所示:
備註:此公式僅適用於 Office 2019、365 及更高版本。
使用 VBA 代碼列出單列中所有可能的組合
以上公式僅適用於較新的 Excel 版本,如果您有較早的 Excel 版本,以下 VBA 代碼可以幫到您。
1。 按 Alt + F11 同時打開 Microsoft Visual Basic for Applications 窗口。
2。 然後,點擊 插入 > 模塊,將以下 VBA 代碼複製並粘貼到窗口中。
VBA 代碼:列出單列中所有可能的組合
Sub ConnectArr()
'Updateby ExtendOffice
Dim xDValue As Variant
Dim xOutRg As Range
Dim xDictionary As Object
Dim xF As Long
Dim xChar As String
xDValue = Range("A2:A6").Value 'the data range
Set xOutRg = Range("C1") 'output range
xChar = "," 'separator
For xF = 1 To UBound(xDValue)
Set xDictionary = CreateObject("Scripting.Dictionary")
xDictionary(0) = "Sets of " & xF
Call ConnectValue(xDValue, xDictionary, 0, xF, 0, "", xChar)
xOutRg.Offset(0, xF - 1).Resize(xDictionary.Count).Value = WorksheetFunction.Transpose(xDictionary.Items)
Set xDictionary = Nothing
Next
End Sub
Sub ConnectValue(ByRef pDValue, ByRef pDictionary, ByRef pLevel, ByVal pMaxLevel, ByVal pIndex, ByVal pValue, ByVal pChar)
Dim xF As Long
If pLevel = pMaxLevel Then
pDictionary(pDictionary.Count + 1) = pValue
Exit Sub
End If
For xF = pIndex + 1 To UBound(pDValue)
If pValue = "" Then
Call ConnectValue(pDValue, pDictionary, pLevel + 1, pMaxLevel, xF, pDValue(xF, 1), pChar)
Else
Call ConnectValue(pDValue, pDictionary, pLevel + 1, pMaxLevel, xF, pValue & pChar & pDValue(xF, 1), pChar)
End If
Next
End Sub
備註:在上面的代碼中:
- A2:A6: 是您要使用的數據列表;
- C1: 是輸出單元格;
- ,: 分隔組合的分隔符。
3. 然後,按 F5 執行此代碼的關鍵。 單列中的所有組合如下圖所示:
最佳辦公生產力工具
🤖 | Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行 | 生成代碼 | 建立自訂公式 | 分析數據並產生圖表 | 呼叫 Kutools 函數... |
熱門特色: 尋找、突出顯示或識別重複項 | 刪除空白行 | 合併列或儲存格而不遺失數據 | 沒有公式的回合 ... | |
超級查詢: 多條件VLookup | 多值VLookup | 跨多個工作表的 VLookup | 模糊查詢 .... | |
高級下拉列表: 快速建立下拉列表 | 依賴下拉列表 | 多選下拉列表 .... | |
欄目經理: 新增特定數量的列 | 移動列 | 切換隱藏列的可見性狀態 | 比較範圍和列 ... | |
特色功能: 網格焦點 | 設計圖 | 大方程式酒吧 | 工作簿和工作表管理器 | 資源庫 (自動文字) | 日期選擇器 | 合併工作表 | 加密/解密單元格 | 按清單發送電子郵件 | 超級濾鏡 | 特殊過濾器 (過濾粗體/斜體/刪除線...)... | |
前 15 個工具集: 12 文本 工具 (添加文本, 刪除字符,...) | 50+ 圖表 類型 (甘特圖,...) | 40+ 實用 公式 (根據生日計算年齡,...) | 19 插入 工具 (插入二維碼, 從路徑插入圖片,...) | 12 轉化 工具 (數字到單詞, 貨幣兌換,...) | 7 合併與拆分 工具 (高級合併行, 分裂細胞,...) | ... 和更多 |
使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。 點擊此處獲取您最需要的功能...
Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆
- 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
- 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
- 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Sort comments by
#44900
This comment was minimized by the moderator on the site
0
0
#43254
This comment was minimized by the moderator on the site
Report
0
0
#43260
This comment was minimized by the moderator on the site
Report
0
0
#42802
This comment was minimized by the moderator on the site
0
0
#43009
This comment was minimized by the moderator on the site
Report
0
0
#40941
This comment was minimized by the moderator on the site
0
0
#40965
This comment was minimized by the moderator on the site
Report
0
0
#40980
This comment was minimized by the moderator on the site
Report
0
0
#40978
This comment was minimized by the moderator on the site
0
0
#40968
This comment was minimized by the moderator on the site
0
0
#41788
This comment was minimized by the moderator on the site
Report
0
0
There are no comments posted here yet