如何通过单词列表增加?

我在VBA中有两个可变元素的“For循环”。 一个是数字,一个是一个字。 想象一下,最终的输出如下所示:

Result[1].Apple Result[1].Orange Result[1].Banana Result[2].Apple Result[2].Orange Result[2].Banana 

对于12 ,在我的函数的开始我使用: i = 1 to 2 。 在我的function结束后,我使用: Next i

 Sub Button1_Click() For i = 0 To 5 'want to cycle through .apple, .orange, .banana after going through i values. presumably calling this variable Fruit MyConcatenatedString = "Result[" & i & "]." & Fruit If TypeName(Data) = "Error" Then If MsgBox("Error reading "Result[" & i & "]." & Fruit & _ "Continue with read?", vbYesNo + vbExclamation, _ "Error") = vbNo Then Exit For Else 'no error, put data in excel cell End If 'send data to cell and increment i Cells(4, 2 + i) = MyConcatenatedString Next i End Sub 

我怎样才能让我的function循环通过AppleOrangeBanana像我循环12使用i

您可以使用数组来定义第二个variables元素:

 Dim A() : Redim A(2)) A(0)="Apple" : A(1)="Orange" : .... 

并循环使用For k = LBound(A) to UBound(A)

所以最后应该看起来像这样:

 Sub test_Karhu() Dim TpStr As String Dim A(): ReDim A(2) A(0) = "Apple": A(1) = "Orange": A(2) = "Banana" For i = 1 To 2 For k = LBound(A) To UBound(A) TpStr = TpStr & "Result[" & i & "]." & A(k) & vbCrLf Next k Next i MsgBox TpStr End Sub