[英]How to use VBA to get permutations of “yes” and “No” across 5 columns in Excel?
[英]How to use excel to get permutations of “Yes” and “No” across 6 columns?
你能告訴我如何獲得 6 列中是和否的所有排列嗎?
問了一個類似的問題,提供了以下5列的公式:
=CHOOSE(1+MID(LEFT("00000",5-LEN(DEC2BIN(32-ROW()+1)))&DEC2BIN(32-ROW()+1),COLUMN()-1,1),"No","Yes")
為 5 列提供了以下 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
獎勵邏輯:
同樣基於二進制模式:
Sub qwerty()
Dim i As Long, wf As WorksheetFunction
Set wf = Application.WorksheetFunction
With wf
For i = 0 To 63
arr = Split(Replace(Replace(frag(.Dec2Bin(i, 6)), "1", "yes"), "0", "no"), ",")
Range(Cells(i + 1, 1), Cells(i + 1, 6)).Value = arr
Next i
End With
End Sub
Public Function frag(sin As String) As String
Dim i As Long, L As Long
frag = Left(sin, 1)
L = Len(sin)
If L = 1 Then Exit Function
For i = 2 To L
frag = frag & "," & Mid(sin, i, 1)
Next i
End Function
Frag()
將字符串轉換為逗號分隔的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.