Reputation: 381
I am trying (in VBA) to define a large number of string variables. The brute force would be :
Dim Port1 as String
Dim Port2 as String
etc…
Unpleasant for say 100 variables. There must be a more intelligent solution. I have tried :
Dim n As Integer
For n = 1 To 100
Dim "Port" & n as String
Next n
and variations of it without success. I would greatly appreciate if someone could point me in the right direction or share an example.
Upvotes: 1
Views: 1445
Reputation: 10328
Arrays do exactly this! See this example:
Dim Port(1 to 100) As String
Dim i As Long
Port(1) = "String"
For i = 2 to 100
Port(i) = "String " & i
Next i
It sets Port(1), the first array element to the word "String". Everything else Port(2)
and on, contain "String 2", "String 3", etc. Up to Port(100)
.
I hope that helps!
Upvotes: 7