ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Fill an Array with String values (https://www.excelbanter.com/excel-programming/298256-fill-array-string-values.html)

John Michl[_2_]

Fill an Array with String values
 
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



Bob Phillips[_6_]

Fill an Array with String values
 
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)

"John Michl" wrote in message
...
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





Bob Kilmer[_2_]

Fill an Array with String values
 
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


"John Michl" wrote in message
...
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





Alan Beban[_2_]

Fill an Array with String values
 
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

John Michl wrote:

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



Dana DeLouis[_3_]

Fill an Array with String values
 
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

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"John Michl" wrote in message
...
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





Alan Beban[_2_]

Fill an Array with String values
 
Dana DeLouis wrote:
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

Norman Jones

Fill an Array with String values
 
Or v = ["String" & Column(A:T)]

---
Regards,
Norman Jones

"Alan Beban" wrote in message
...
Dana DeLouis wrote:
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





All times are GMT +1. The time now is 04:37 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com