2017-04-19 240 views
0

我有一個數組創建一個數組陣列陣列VBA

這裏我的代碼:

Dim servers(2) As String 
Dim parameters(3) As String 
Dim i, j As Integer 

parameters(0) = "db1" 
parameters(1) = "server1" 
parameters(2) = "user1" 
parameters(3) = "password1" 

For i = 0 To 2 
    For j = 0 To 3 
     servers(i) = parameters(j) 
    Next j 
Next i 

我做了一個圖形化的例子,結果,我真的不知道該怎麼做: https://i.stack.imgur.com/1uSme.png

回答

1

你居然需要一個2-d數組來填充你的servers,試試下面的代碼:

Option Explicit 

Sub x() 

Dim servers(2, 3) As String 
Dim parameters() As Variant 
Dim i As Long, j As Long 

parameters = Array("db", "server", "user", "password") 

For i = 0 To 2 
    For j = LBound(parameters) To UBound(parameters) 
     servers(i, j) = parameters(j) & i + 1 
    Next j 
Next i 

End Sub 
0

嘗試使用二維數組:

Dim parameters(10,10) As String 

parameters(1,1) = "db1" 
parameters(1,2) = "server1" 
... 
parameters(2,1) = "db2" 
parameters(2,2) = "server2" 
... 
4

這似乎是一個用戶定義類型可以幫助您:

Type tServerData 
    DBname As String 
    ServerID As String 
    UserName As String 
    Password As String 
End Type 

Dim ServerData(0 To 2) As tServerData 

現在你有3臺服務器的arrray,例如:

ServerData(0).DBname= "db1" 

+0

UDT的是如此被低估。這應該是被接受的答案。 –