Note: The other languages of the website are Google-translated. Back to English
登陆  \/ 
x
or
x
点此注册  \/ 
x

or

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

假设我有以下两列数据,现在,我想基于两个值列表生成所有可能组合的列表,如左图所示。 也许,如果值很少,则可以一一列出所有组合,但是,如果有几列需要列出多个值,可能的组合就可以通过下面的一些快速技巧来解决。 。

用公式列出或生成两个列表中的所有可能组合

使用VBA代码列出或生成来自三个或更多列表的所有可能组合

列出或生成具有强大功能的多个列表中的所有可能组合


用公式列出或生成两个列表中的所有可能组合

下面的长公式可以帮助您快速列出两个列表值的所有可能组合,请执行以下操作:

1。 将以下公式输入或复制到空白单元格中,在这种情况下,我将其输入到单元格D2中,然后按 输入 获得结果的关键,请参见屏幕截图:

=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应用程序 窗口。

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.