解析多个数组并编写所有可能的组合

时间:2016-09-14 18:24:52

标签: arrays excel vba

我正在尝试在Excel中使用VBA将三个数组内容的所有可能组合写入一列以创建一个词表。

目前,我知道如何循环遍历数组并得到我想要的输出但我无法弄清楚如何构建循环以给我所有可能的baseWord(n)和&组合。 numberCharSet(n)& specialCharSet(n)的

如何正确遍历baseWord数组以获取每个baseWord与specialCharSet和numberCharSet数组内容的所有组合?

实施例: Cloud1! Cloud1 @ Cloud1# Cloud1 $ Cloud2! 等...

Private Sub createWordlist()

Dim baseWord(1 To 2) As String
baseWord(1) = "Cloud"
baseWord(2) = "cloud"

Dim numberCharSet(1 To 4) As String
numberCharSet(1) = "1"
numberCharSet(2) = "2"
numberCharSet(3) = "3"
numberCharSet(4) = "4"


Dim specialCharSet(1 To 4) As String
specialCharSet(1) = "!"
specialCharSet(2) = "@"
specialCharSet(3) = "#"
specialCharSet(4) = "$"

x = 1
y = 1
z = 4
w = 1

For Each Item In baseWord
    Range("A" & x).Value = baseWord(w) & numberCharSet(y) & specialCharSet(z)
    x = x + 1
    y = y + 1
    z = z - 1
Next

End Sub

1 个答案:

答案 0 :(得分:2)

正如@ScottCraner在评论中提到的,你需要做的就是嵌套3循环:

For Each word In baseWord
    For Each num In numberCharSet
        For Each special In specialCharSet
            Debug.Print word & num & special
        Next
    Next
Next