String separated with commas

J

Jarle

I have a string looking like:

aaa, bbb, ccc, ddd, eee.....

I want to load an array with these elements - like:

Array(aaa,bbb,ccc,ddd,eee.........).

The elements is separated with comma and space in string.

What is the best way to do this?
 
G

Gary''s Student

Sub dural()
s = "a, b, c, d, e"
s2 = Split(Replace(s, " ", ""), ",")
End Sub

Here s2 is an array. s2(0) will contain "a", etc.
 
J

Jarle

Fantastic - even more elegant that could ever imagine.
Thank you!

Gary''s Student skrev:
 
G

Gary''s Student

You are very welcome. Split is nice because it creates an array without a DIM.


Join() is the opposite.
 
R

Rick Rothstein \(MVP - VB\)

Sub dural()
s = "a, b, c, d, e"
s2 = Split(Replace(s, " ", ""), ",")
End Sub

You do not have to use the replace function to pare down the delimiter to a
single character like that... the delimiter can contain multiple
characters...

s2 = Split(s, ", ")

where the characters between the quote marks are a comma followed by a
space. Now, with that said, doing what you posted will protect against a
improperly formed string of text where the number of spaces following the
comma are not consistent throughout the text.

Rick
 
G

Gary''s Student

Hi Rick:

Thanks for the tip about being able to use more than one character as a
separator.
 

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