Fill an Array with String values

J

John Michl

Simple question. How do I load a list of strings into an array?

I know I code so the following but it seems inefficient.

Dim a (1 to 20) as String

a(1) = "String1"
a(2) = "String2"
etc...

Can I do something like...

a(1 to 20) = "String1", "String2", "String3" ....."String20"

or

a = Array("String1", "String2", "String3" ....."String20" )

Thanks.

- John Michl
www.JohnMichl.com
 
B

Bob Phillips

John,

Dim a

a = Array("String1", "String2", "String3" ....."String20" )

Don't pre-dimension it.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
B

Bob Kilmer

For 20 values, I am not sure the individual assignment approach is
particularly costly.

Option Explicit

Sub main()
Dim v As Variant, i As Integer
v = Split("abc,def,ghi,jkl", ",")
Debug.Print "------------"
For i = LBound(v) To UBound(v)
Debug.Print v(i)
Next
Debug.Print "------------"
v = Array("mno", "pqr", "stu", "vwx")
For i = LBound(v) To UBound(v)
Debug.Print v(i)
Next
End Sub
 
A

Alan Beban

You can do one of a number of things. E.g.:

Dim a
a = Array("String1", "String2", "String3")

a will seem to be an Array of type Variant() [i.e., that's what
Typename(a) will return], though many (most?) people describe it as a
Variant variable containing an array of strings. Nevertheless,
If TypeName(a) = "Variant" Then
MsgBox "Variant variable"
Else
MsgBox "Variant array"
End If

will display Variant array.

In current versions, xl2000 and later, you can use

Dim a()
a = Array("String1", "String2", "String3" ....."String20")
Once again, Typename(a) will return Variant()

If the functions in the file at http://home.pacbell.net/beban are
available to your workbook, you can use

Dim a() As String
Assign Array("String1", "String2", "String3"), a

in which case a will be an array of type String()

Alan Beban
 
D

Dana DeLouis

For those who don't like loops per say, here is a "Poor mans" version of
List processing...

Sub Demo()
'// Dana DeLouis
Dim v
v = [Transpose("String" & Row(A1:A20))]
End Sub
 
A

Alan Beban

Dana said:
For those who don't like loops per say, here is a "Poor mans" version of
List processing...

Sub Demo()
'// Dana DeLouis
Dim v
v = [Transpose("String" & Row(A1:A20))]
End Sub

or v = ["String" & Column(A1:T1)]

Alan Beban
 
N

Norman Jones

Or v = ["String" & Column(A:T)]

---
Regards,
Norman Jones

Alan Beban said:
Dana said:
For those who don't like loops per say, here is a "Poor mans" version of
List processing...

Sub Demo()
'// Dana DeLouis
Dim v
v = [Transpose("String" & Row(A1:A20))]
End Sub

or v = ["String" & Column(A1:T1)]

Alan Beban
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top