使用循环VBA将值分配给variables

我有variables声明在VBAfunction像A1,A2,A3 …..安

并将值分配给A1,A2,A3 … An

现在我怎样才能使用循环更新variables的值

Dim A1, A2, A3 As Integer, Tmp As String A1 = 1 A2 = 2 A3 = 3 For i = 1 To 3 Debug.Print A & i A & i = i+1 --- This line is not working Next 

如何在不使用任何数组的情况下分配variables

重新考虑使用数组:

 Sub marine() Dim A(1 To 3) As Long A(1) = 1 A(2) = 2 A(3) = 3 End Sub 

您可以创build一个集合来执行此操作,稍后循环集合或通过传入键(variables名称)来获取值

 Sub TestCollection() Dim i As Long Dim objCollection As New Collection Dim varVariable As Variant 'Loop From 1 To 3. The Upper Bound Can Be Changed To Suit Your Needs For i = 1 To 3 'Add The Key And Item To The Collection 'The First Parameter Is The Item (Value You Want To Store) 'The Second Parameter Is The Key (How You Access The Value, Like A Variable Name) objCollection.Add i, "A" & i Next i 'The Value Passed Into The Collection Is The Key - Which Is Like The Variable Name Debug.Print objCollection("A1") Debug.Print objCollection("A2") Debug.Print objCollection("A3") 'Loop All Values For Each varVariable In objCollection Debug.Print varVariable Next varVariable End Sub