Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

如何在Excel中列出或生成所有可能的組合?

假設我有以下兩列數據,現在,我想基於兩個值列表生成所有可能組合的列表,如左圖所示。 也許,如果值很少,則可以一一列出所有組合,但是,如果有幾列需要列出多個值,則可能是組合,這裡有一些快速技巧可以幫助您解決Excel中的此問題。

用公式列出或生成兩個列表中的所有可能組合

使用VBA代碼列出或生成來自三個或更多列表的所有可能組合

列出或生成具有強大功能的多個列表中的所有可能組合


用公式列出或生成兩個列表中的所有可能組合

下面的長公式可以幫助您快速列出兩個列表值的所有可能組合,請執行以下操作:

1。 輸入以下公式或將其複製到空白單元格中,在這種情況下,我將其輸入到單元格D2中,然後按 Enter 獲得結果的關鍵,請參見屏幕截圖:

=IF(ROW()-ROW($D$2)+1>COUNTA($A$2:$A$5)*COUNTA($B$2:$B$4),"",INDEX($A$2:$A$5,INT((ROW()-ROW($D$2))/COUNTA($B$2:$B$4)+1))&"-"&INDEX($B$2:$B$4,MOD(ROW()-ROW($D$2),COUNTA($B$2:$B$4))+1))

備註:在以上公式中, $ A $ 2:$ A $ 5 是第一列值的範圍,並且 $ B $ 2:$ B $ 4 是您要列出所有可能組合的第二個列表值的範圍, $ D $ 2 是您輸入公式的單元格,則可以根據需要更改單元格引用。

2。 然後選擇單元格D2,並將填充手柄向下拖動到單元格,直到獲得空白單元格,並且所有可能的組合均已基於兩個列表值列出。 看截圖:


使用VBA代碼列出或生成來自三個或更多列表的所有可能組合

也許上面的公式對您來說很難應用,如果有多列數據,則修改起來會很麻煩。 在這裡,我將介紹一個VBA代碼來快速處理它。

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:生成3列或多列的所有組合

Sub ListAllCombinations()
'Updateby Extendoffice
Dim xDRg1, xDRg2, xDRg3 As Range
Dim xRg  As Range
Dim xStr As String
Dim xFN1, xFN2, xFN3 As Integer
Dim xSV1, xSV2, xSV3 As String
Set xDRg1 = Range("A2:A5")  'First column data
Set xDRg2 = Range("B2:B4")  'Second column data
Set xDRg3 = Range("C2:C4")  'Third column data
xStr = "-"   'Separator
Set xRg = Range("E2")  'Output cell
For xFN1 = 1 To xDRg1.Count
    xSV1 = xDRg1.Item(xFN1).Text
    For xFN2 = 1 To xDRg2.Count
        xSV2 = xDRg2.Item(xFN2).Text
      For xFN3 = 1 To xDRg3.Count
        xSV3 = xDRg3.Item(xFN3).Text
        xRg.Value = xSV1 & xStr & xSV2 & xStr & xSV3
        Set xRg = xRg.Offset(1, 0)
       Next
    Next
Next
End Sub
備註:在上面的代碼中, A2:A5, B2:B4, C2:C4 是您要使用的數據范圍, E2 是您要查找結果的輸出單元格。 如果要獲得更多列的所有組合,請根據需要更改並將其他參數添加到代碼中。

3。 然後,按 F5 鍵來運行此代碼,並且將立即生成3列的所有組合,請參見屏幕截圖:


列出或生成具有強大功能的多個列表中的所有可能組合

如果有多個列表值需要列出可能的組合,則可能很難修改代碼。 在這裡,我可以推荐一個功能強大的工具- Kutools for Excel,它包含一個方便的功能 列出所有組合 可以根據給定的數據列表快速列出所有可能的組合。

保養竅門:要應用此 列出所有組合 功能,首先,您應該下載 Kutools for Excel,然後快速輕鬆地應用該功能。

安裝後 Kutools for Excel,請這樣做:

1. 點擊 Kutools > 插入 > 列出所有組合,請參見屏幕截圖:

2。 在 列出所有組合 對話框中,進行如下所示的演示操作:

3。 然後,所有指定的值和分隔符都已列在對話框中,請參見屏幕截圖:

4然後單擊 Ok 按鈕,然後會彈出一個提示框,提醒您選擇一個單元格以輸出結果,請參見屏幕截圖:

5。 點擊 OK,已根據工作表生成了基於給定列表的所有可能組合,如以下屏幕截圖所示:

單擊以立即下載Kutools for Excel!


更多相關文章:

  • 生成3列或多列的所有組合
  • 假設我有3列數據,現在,我想在這3列中生成或列出數據的所有組合,如下圖所示。 您有什麼好的方法可以在Excel中解決此任務?
  • 查找等於給定總和的所有組合
  • 例如,我有以下數字列表,現在,我想知道列表中的數字組合總計為480,在下面的屏幕截圖中,您可以看到有五組可能的組合加起來等於到480,例如300 + 60 + 120、300 + 60 + 40 + 80等。本文,我將討論一些方法來查找哪些單元格在Excel中求和成特定值。
  • 生成或列出所有可能的排列
  • 例如,我有三個字符XYZ,現在,我想基於這三個字符列出所有可能的排列,以獲得六個不同的結果,例如:XYZ,XZY,YXZ,YZX,ZXY和ZYX。 在Excel中,如何根據不同的字符數快速生成或列出所有排列?
  • 生成所有可能的4位數字組合的列表
  • 在某些情況下,我們可能需要生成一個由4到0的所有可能的9位數字組成的列表,這意味著要生成一個0000、0001、0002…9999的列表。 為了快速解決Excel中的列表任務,我為您介紹了一些技巧。

最佳辦公效率工具

Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及您以前使用過的任何東西; 加密單元 帶密碼 創建郵件列表 並發送電子郵件...
  • 超級公式欄 (輕鬆編輯多行文本和公式); 閱讀版式 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不會丟失數據; 拆分單元格內容; 合併重複的行/列...防止細胞重複; 比較範圍...
  • 選擇重複或唯一 行; 選擇空白行 (所有單元格都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇...
  • 確切的副本 多個單元格,無需更改公式參考; 自動創建參考 到多張紙; 插入項目符號,複選框等...
  • 提取文字,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級濾鏡 (將過濾方案保存並應用於其他工作表); 高級排序 按月/週/日,頻率及更多; 特殊過濾器 用粗體,斜體...
  • 結合工作簿和工作表; 根據關鍵列合併表; 將數據分割成多個工作表; 批量轉換xls,xlsx和PDF...
  • 超過300種強大功能。 支持Office / Excel 2007-2019和365。支持所有語言。 在您的企業或組織中輕鬆部署。 完整功能30天免費試用。 60天退款保證。
kte選項卡201905

Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 每天將您的工作效率提高50%,並減少數百次鼠標單擊!
officetab底部
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Juan Tomás · 1 years ago
    Excelentes soluciones, muchas gracias, lo logré con Kutools
  • To post as a guest, your comment is unpublished.
    RAFFAELE · 1 years ago
    NON FUNZIONA. HO SEGUITO GLI STESSI PASSAGGI MA MI DICE CHE C'E' UN ERRORE
  • To post as a guest, your comment is unpublished.
    Guy · 1 years ago
    Is there a way to output this as a txt file? When you have millions of results in the D column it's not exactly practical to drag the fill handle.
  • To post as a guest, your comment is unpublished.
    JJ · 2 years ago
    Almost very handy. Would be good if it actually used whatever is in the cell ($A$1)and not convert the cell to text. Then I could just paste what I need in A1 and run it again without having to change anything.
  • To post as a guest, your comment is unpublished.
    pocholoclub@gmail.com · 2 years ago
    I used the extions and works fantastic,, but now i have a problem, i need to calculate the sum,, but the result is a text,, i enter a separator that is the simbol +, but finally i have a text and is impossible to convert in a operation.
  • To post as a guest, your comment is unpublished.
    sergio · 2 years ago
    Hello, and if these combinations intead text are number,, how can i made the calculation for the results???? i try to add the "+" but excel dont know that is the simbol.. how you solve this?
  • To post as a guest, your comment is unpublished.
    ricardo · 2 years ago
    Hola me arroja una referencia circula la formula
    =SI(FILA()-FILA($D$1)+1>(CONTARA($A$1:$A$4)*CONTARA($B$1:$B$3));"";INDICE($A$1:$A$4;RESIDUO((FILA()-FILA($D$1));COUNTA($B$1:$B$3)+1))
    &INDICE($B$1:$B$3;RESIDUO(FILA()-FILA($D$1);CONTARA($B$1:$B$3))+1))
  • To post as a guest, your comment is unpublished.
    anirbank · 3 years ago
    Why my formula did not work to create 25 combinations in Column E (5 elements from Col A * 5 elements from Col B) ? Whereas the formula in this article worked in Column D when I dragged it for 25 cells? Screenshot - https://prnt.sc/ihwr18
  • To post as a guest, your comment is unpublished.
    Mihai · 3 years ago
    Hi,
    I want to know what to do if I want to generate more than 1,4 million combination and I exceede the Excel rows limit?
    Is there a way of puting Kudos to continue its iteration on the next column?
  • To post as a guest, your comment is unpublished.
    Marco · 3 years ago
    Has anyone figured out a formula yet to do this for 5 sets of data? I've been stumped on this so far.
    • To post as a guest, your comment is unpublished.
      skyyang · 3 years ago
      Hello, Marco,
      The Kutools can help you to solve your problem quickly,please check the following screenshot:
      Please try it, hope it can help you, thank you!
  • To post as a guest, your comment is unpublished.
    acomuna · 3 years ago
    Is KuTools able to sort cells in a row in a "simple combination" fashion?
    I mean, if I have these data:
    _________________________
    John Jack Paul Macy

    Mark Larry

    Jerry Paul Mary

    Sam Jeff Peter Lucas
    _________________________



    And I want to output them like this:
    ___________
    John Jack

    John Paul

    John Macy

    Jack Paul

    Jack Macy

    Paul Macy

    Mark Larry

    Jerry Paul

    Jerry Marry

    Paul Mary

    Sam Jeff

    Sam Peter

    Sam Lucas

    Jeff Peter

    Jeff Lucas

    Peter Lucas
    ____________


    How can I do this? Can KuTools do sth like this?

    Thanks!
    • To post as a guest, your comment is unpublished.
      skyyang · 3 years ago
      Hi, Arthur,
      Sorry, the Kutools can not help you to solve this job as you said.
      Thank your for your comment.
  • To post as a guest, your comment is unpublished.
    Eoin · 3 years ago
    Any idea how to add a space inbetween ?
    • To post as a guest, your comment is unpublished.
      Mary · 3 years ago
      You can add a space by adding " "& before you index column B, right after the existing &, like so.....

      =IF(ROW()-ROW($D$1)+1>COUNTA($A$1:$A$4)*COUNTA($B$1:$B$3),"",INDEX($A$1:$A$4,INT((ROW()-ROW($D$1))/COUNTA($B$1:$B$3)+1))&" "&INDEX($B$1:$B$3,MOD(ROW()-ROW($D$1),COUNTA($B$1:$B$3))+1))


      ....or any other delimiter you like!
    • To post as a guest, your comment is unpublished.
      skyyang · 3 years ago
      Hello, Eoin,
      the formula in this article can not help you to add a space between the cell values, but, you can apply the Kutools for Excel, with it, you can type any delimiter as you want to separate the combined result, see screenshot:
  • To post as a guest, your comment is unpublished.
    JiffyLimbo · 3 years ago
    For more columns:
    The first part of the formulate needs to be modified to multiply all the possibilities, for example this would be for 6 columns:

    COUNTA($A$2:$A$9)*COUNTA($B$2:$B$9)*COUNTA($C$2:$C$9)*COUNTA($D$2:$D$9)*COUNTA($E$2:$E$9)*COUNTA($F$2:$F$9)
    so you are adding
    *COUNTA(YourRangeHere)
    for each column

    The second part of the formula needs to be modified for each column as well like so:

    INDEX($A$2:$A$9,MOD(ROW()-ROW($I$2),COUNTA($A$2:$A$9))+1)&INDEX($B$2:$B$9,MOD(ROW()-ROW($I$2),COUNTA($B$2:$B$9))+1)&INDEX($C$2:$C$9,MOD(ROW()-ROW($I$2),COUNTA($C$2:$C$9))+1)&INDEX($D$2:$D$9,MOD(ROW()-ROW($I$2),COUNTA($D$2:$D$9))+1)&INDEX($E$2:$E$9,MOD(ROW()-ROW($I$2),COUNTA($E$2:$E$9))+1)&INDEX($F$2:$F$9,MOD(ROW()-ROW($I$2),COUNTA($F$2:$F$9))+1)

    so you are adding

    &INDEX(YourRangeHere,MOD(ROW()-ROW($I$2),COUNTA(YourRangeHere))+1)

    So put them all together and you get this example for 6 columns:

    =IF(ROW()-ROW($I$2)+1>COUNTA($A$2:$A$9)*COUNTA($B$2:$B$9)*COUNTA($C$2:$C$9)*COUNTA($D$2:$D$9)*COUNTA($E$2:$E$9)*COUNTA($F$2:$F$9),"",INDEX($A$2:$A$9,MOD(ROW()-ROW($I$2),COUNTA($A$2:$A$9))+1)&INDEX($B$2:$B$9,MOD(ROW()-ROW($I$2),COUNTA($B$2:$B$9))+1)&INDEX($C$2:$C$9,MOD(ROW()-ROW($I$2),COUNTA($C$2:$C$9))+1)&INDEX($D$2:$D$9,MOD(ROW()-ROW($I$2),COUNTA($D$2:$D$9))+1)&INDEX($E$2:$E$9,MOD(ROW()-ROW($I$2),COUNTA($E$2:$E$9))+1)&INDEX($F$2:$F$9,MOD(ROW()-ROW($I$2),COUNTA($F$2:$F$9))+1))

    Where you would paste this formula into $I$2 and it would be looking at $A$2:$A$9 $B$2:$B$9 $C$2:$C$9 ... up to $F$2:$F$9
    • To post as a guest, your comment is unpublished.
      JiffyLimbo · 3 years ago
      this doesn't work :( more complicated than this
  • To post as a guest, your comment is unpublished.
    JiffyLimbo · 3 years ago
    For more columns:
    The first part of the formulate needs to be modified to multiply all the possibilities, for example this would be for 6 columns
    COUNTA($A$2:$A$9)*COUNTA($B$2:$B$9)*COUNTA($C$2:$C$9)*COUNTA($D$2:$D$9)*COUNTA($E$2:$E$9)*COUNTA($F$2:$F$9)
    so you are adding
    *COUNTA(YourRangeHere)
    for each column
    The second part of the formula needs to be modified for each column as well like so:
    INDEX($A$2:$A$9,MOD(ROW()-ROW($I$2),COUNTA($A$2:$A$9))+1)&INDEX($B$2:$B$9,MOD(ROW()-ROW($I$2),COUNTA($B$2:$B$9))+1)&INDEX($C$2:$C$9,MOD(ROW()-ROW($I$2),COUNTA($C$2:$C$9))+1)&INDEX($D$2:$D$9,MOD(ROW()-ROW($I$2),COUNTA($D$2:$D$9))+1)&INDEX($E$2:$E$9,MOD(ROW()-ROW($I$2),COUNTA($E$2:$E$9))+1)&INDEX($F$2:$F$9,MOD(ROW()-ROW($I$2),COUNTA($F$2:$F$9))+1)
    so you are adding
    &INDEX(YourRangeHere,MOD(ROW()-ROW($I$2),COUNTA(YourRangeHere))+1)

    So put them all together and you get this example for 6 columns:

    =IF(ROW()-ROW($I$2)+1>COUNTA($A$2:$A$9)*COUNTA($B$2:$B$9)*COUNTA($C$2:$C$9)*COUNTA($D$2:$D$9)*COUNTA($E$2:$E$9)*COUNTA($F$2:$F$9),"",INDEX($A$2:$A$9,MOD(ROW()-ROW($I$2),COUNTA($A$2:$A$9))+1)&INDEX($B$2:$B$9,MOD(ROW()-ROW($I$2),COUNTA($B$2:$B$9))+1)&INDEX($C$2:$C$9,MOD(ROW()-ROW($I$2),COUNTA($C$2:$C$9))+1)&INDEX($D$2:$D$9,MOD(ROW()-ROW($I$2),COUNTA($D$2:$D$9))+1)&INDEX($E$2:$E$9,MOD(ROW()-ROW($I$2),COUNTA($E$2:$E$9))+1)&INDEX($F$2:$F$9,MOD(ROW()-ROW($I$2),COUNTA($F$2:$F$9))+1))

    Where you would paste this formula into $I$2 and it would be looking at $A$2:$A$9 $B$2:$B$9 $C$2:$C$9 ... up to $F$2:$F$9
    • To post as a guest, your comment is unpublished.
      JiffyLimbo · 3 years ago
      this doesnt work :( more complicated than this
  • To post as a guest, your comment is unpublished.
    Javier · 4 years ago
    anyone know how to modify this to reflect 6 columns of data?
  • To post as a guest, your comment is unpublished.
    Javier · 4 years ago
    anyone know how to modify this to reflect 6 columns of data, rather than just two?
    • To post as a guest, your comment is unpublished.
      shey · 3 years ago
      looking for the same thing. anyone know?
  • To post as a guest, your comment is unpublished.
    NB · 4 years ago
    How could you use KuTools, or even a formula, if you wanted all the permutations of the Month header. Jan, Jan and Feb, Jan and Mar, Jan and Mar, Jan and Feb and Mar, etc etc
  • To post as a guest, your comment is unpublished.
    jitu · 4 years ago
    VERY HELPFUL.I am now able to generate combinations very easily.
  • To post as a guest, your comment is unpublished.
    Veerendra Kumar · 4 years ago
    Can I get the formula for all the combinations of a 5*5 matrix (5 rows and 5 columns ) I tried but not able to get please help me.......
  • To post as a guest, your comment is unpublished.
    Free · 4 years ago
    Is there a way to make this formula work where it produces the various combinations still separated in 2 separate columns, but adjacent to one another?
    • To post as a guest, your comment is unpublished.
      Bill · 4 years ago
      Have you had any luck with this yet? I'm trying to do the same thing and have all of the possible combinations next to each other in a 2 separate columns.
    • To post as a guest, your comment is unpublished.
      Bill · 4 years ago
      Have you had any luck figuring out how to do this? I'm trying to do the exact same thing and have all of my probabilities in 2 separate columns in excel.
  • To post as a guest, your comment is unpublished.
    GATES · 4 years ago
    example : my list of values is 1,2,3,4,5,6......80 , i want kutool to display all combinations of a set of 2 numbers, example : 1-2 , 1-3 , 1-4 , 1-5 , ..................79-80 . is this possible with KUTOOL ?
  • To post as a guest, your comment is unpublished.
    Kyle · 5 years ago
    This is a brilliant formula! I have no clue how it works, but I just change the collum A and B to match how long my lists are, and have my output in D1.