2016-09-14 173 views
0

我想在Excel中使用VBA將三個數組的內容的所有可能組合寫入列以創建一個單詞表。我知道如何遍歷數組,並獲得一些我想要的輸出,但我無法弄清楚如何構建循環,以便給出baseWord(n)的所有可能組合。numberCharSet(n)& numberCharSet & specialCharSet(n)。解析多個數組並寫入所有可能的組合

如何正確地循環baseWord數組以獲得每個baseWord與specialCharSet和numberCharSet數組的內容的所有組合?

例如: Cloud1! Cloud2 @ 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

需要三個嵌套循環爲每個數組。 –

回答

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