不知道有没有人分享过,示例是4个元素,实际上元素数量可变
Sub test()
Dim arr As Variant, StrArr() As Variant
Dim i As Integer, j As Long, k As Long, l As Integer
Dim Str As String
arr = Array("A", "B", "C", "D")
i = UBound(arr) + 1
j = 2 ^ i - 1
For k = 1 To j
For l = 1 To i
If (2 ^ (l - 1) And k) > 0 Then
If Str = Empty Then
Str = arr(l - 1)
Else
Str = Str & "+" & arr(l - 1)
End If
End If
Next l
ReDim Preserve StrArr(1 To k)
StrArr(k) = Str
Str = Empty
Next k
Stop
msgbox Join(StrArr,vbcrlf)
End Sub
Sub test()
Dim arr As Variant, StrArr() As Variant
Dim i As Integer, j As Long, k As Long, l As Integer
Dim Str As String
arr = Array("A", "B", "C", "D")
i = UBound(arr) + 1
j = 2 ^ i - 1
For k = 1 To j
For l = 1 To i
If (2 ^ (l - 1) And k) > 0 Then
If Str = Empty Then
Str = arr(l - 1)
Else
Str = Str & "+" & arr(l - 1)
End If
End If
Next l
ReDim Preserve StrArr(1 To k)
StrArr(k) = Str
Str = Empty
Next k
Stop
msgbox Join(StrArr,vbcrlf)
End Sub