简体   繁体   English

如何使用 VBA 在 Excel 的 5 列中获得“是”和“否”的排列?

[英]How to use VBA to get permutations of “yes” and “No” across 5 columns in Excel?

I need to list all the permutations of Yes/No across five columns.我需要在五列中列出是/否的所有排列。 For example, {Yes,Yes,Yes,Yes,Yes}, {Yes,Yes,Yes,Yes,No}, {Yes,Yes,Yes,No,No}, {Yes,Yes,No,No,No}, etc... see this screenshot例如,{是,是,是,是,是},{是,是,是,是,否},{是,是,是,否,否},{是,是,否,否,否}等...看这个截图

Problem is I only came up with those few and I think the total is 120... Is there some VBA code I can run to get ALL the permutations?问题是我只想出了那几个,我认为总数是 120... 是否有一些 VBA 代码我可以运行以获得所有排列?

You don't need VBA to do this.您不需要 VBA 来执行此操作。 The following worksheet formula gets you there.以下工作表公式可助您实现目标。

Select cell B2 and paste this formula: Select 单元格B2并粘贴此公式:

=CHOOSE(1+MID(LEFT("00000",5-LEN(DEC2BIN(32-ROW()+1)))&DEC2BIN(32-ROW()+1),COLUMN()-1,1),"No","Yes")

Now copy that formula to this range: B2:F33现在将该公式复制到此范围: B2:F33

That's it.而已。

Of course, afterward, you may want to copy the results and Paste As Values to have hard values in the cells instead of formulas.当然,之后,您可能希望复制结果并粘贴为值以在单元格中而不是公式中具有硬值。


And here is how to do it in VBA, should you prefer a coded solution:如果您更喜欢编码解决方案,这里是如何在 VBA 中执行此操作:

Option Explicit

Sub DisplayYesNoPerms()
    Dim rows&, cols&
    cols = 5
    rows = 2 ^ cols
    [b2].Resize(rows, cols) = YesNoPerms(rows, cols)
End Sub

Function YesNoPerms(rows&, cols&)
    Dim i&, j&, t$, v
    ReDim v(0 To rows - 1, 1 To cols)
    For i = 0 To rows - 1
        t = Format(DecToBin(i), String(cols, "0"))
        For j = 1 To cols
            v(i, j) = IIf(Mid(t, j, 1), "No", "Yes")
        Next
    Next
    YesNoPerms = v
End Function

Function DecToBin$(ByVal n&)
    Do
        DecToBin = n Mod 2 & DecToBin
        n = n \ 2
    Loop While n
End Function

I won't give you a VBA solution, but an algorithm that you can implement: just think of every "No" as a number 0, and a "Yes" as a number 1. You create all numbers in the binary system, from "00000" to "11111" (so from 0 to 31).我不会为您提供 VBA 解决方案,而是您可以实现的算法:只需将每个“否”视为数字 0,将“是”视为数字 1。您在二进制系统中创建所有数字,从“00000”到“11111”(所以从0到31)。

So, what you need to do:所以,你需要做的是:

You take every number from 0 to 31, and for every number:
  You convert it into binary format.
  In that binary format, replace every 0 by a "No" and every 1 by a "Yes".
  Find a way to fill your Excel sheet with the corresponding results.

Good luck祝你好运

This should print all combinations to the respective rows/columns.这应该将所有组合打印到相应的行/列。 User is required to select the top left cell just below the header.用户需要 select header 正下方的左上角单元格。

Dim i As Long, ii As Long, iii As Long, iiii As Long, iiiii As Long
Dim j As Long, jj As Long
Dim str As String
Dim rng As Range
Dim arr(1) As String
arr(0) = "yes"
arr(1) = "no"
Dim arr_rows() As String
Dim arr_columns() As String

' five nested loops (each for a column)
' the idea is to have all possibilities (yes or no) covered
' each row is taken care of by adding vbNewLine
For i = LBound(arr) To UBound(arr)
 For ii = LBound(arr) To UBound(arr)
  For iii = LBound(arr) To UBound(arr)
   For iiii = LBound(arr) To UBound(arr)
    For iiiii = LBound(arr) To UBound(arr)
     str = str & arr(i) & "," & arr(ii) & "," & arr(iii) & "," & arr(iiii) & "," & arr(iiiii) & vbNewLine
    Next iiiii
   Next iiii
  Next iii
 Next ii
Next i

' splitting the string created in the loop by new line character
' (i.e. obtaining an array with an element for each row)
' for each row splitting by a comma
' (i.e. obtaining another array with an element for each column)
arr_rows = Split(str, vbNewLine)
Set rng = Selection
For j = LBound(arr_rows) To UBound(arr_rows)
    arr_columns = Split(arr_rows(j), ",")
    For jj = LBound(arr_columns) To UBound(arr_columns)
        rng.Offset(j, jj) = arr_columns(jj)
    Next jj
Next j

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM