Split string into array

gauntletxg

Well-known Member
Joined
Jul 15, 2008
Messages
636
Say I have a string, "a test array." I want to split this into an array where each character is an element. I was thinking something like

Code:
Dim MyArray As Variant
Dim MyString As String
MyString = "a test array"
MyArray = Split(MyString, "", Len(MyString), vbTextCompare)

But this doesn't work because Split() returns the entire string when the delimiter is a zero-length string. How would I go about doing this?

Thanks.
 
Say I have a string, "a test array." I want to split this into an array where each character is an element. I was thinking something like

Code:
Dim MyArray As Variant
Dim MyString As String
MyString = "a test array"
MyArray = Split(MyString, "", Len(MyString), vbTextCompare)

But this doesn't work because Split() returns the entire string when the delimiter is a zero-length string. How would I go about doing this?

Thanks.
Perhaps I'm missing something but is the delimiter a zero-length string?:eek:

Isn't it " "?
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
No, it wouldn't be " ", the op doesn't to split words, he wants to split by character, in other words from the string "a test array"

This should return an array of the following
a
(space)
t
e
s
t
(space)
a
r
r
a
y
 
Upvote 0
HOTPEPPER

Yeah, kind of realised that after a reread.:oops:

Mind you seems like a strange request, to me anyway.:)
 
Upvote 0
Might also be worth taking a look into regular expressions.:)
 
Upvote 0
Nice function, Fencliff.

Some observations:

1 - It seems the function returns 1 element too much?

Maybe:

Code:
Public Function StringToCharArray(ByRef sIn As String) As String()
    StringToCharArray = Split(StrConv(sIn, vbUnicode), Chr(0))
    If UBound(StringToCharArray) > 0 Then _
        ReDim Preserve StringToCharArray(UBound(StringToCharArray) - 1)
End Function

2 - It will work for ansi characters but not for Unicode characters. If you have a really Unicode output maybe you really have to loop
 
Upvote 0
Nice formula Fencliff. Any idea why Unicode strings are treated differrently by Split() in Vba? Or, rather, how in the world did you know that... :)

One small detail - the array returned has a vbNull string ending the array (which I guess makes it like a C character sequence). Maybe in a vba routine you'd need to ignore this last element in certain types of loops.

--AB
 
Upvote 0
Nice formula Fencliff. Any idea why Unicode strings are treated differrently by Split() in Vba? Or, rather, how in the world did you know that... :)

One small detail - the array returned has a vbNull string ending the array (which I guess makes it like a C character sequence). Maybe in a vba routine you'd need to ignore this last element in certain types of loops.

--AB

VBA internally stores strings in unicode - the strconv function using the arguments as specified outputs a byte array of each character (2 bytes per character as per unicode) separated by a Chr(0). You can assign byte arrays to strings and strings to byte arrays in VBA using straightforward variable assignment eg:

Code:
Dim b() As Byte
Dim strSomeString As String

strSomeString = "This is a string"

'assign to byte array:

b = strSomeString

'check output:

for i = LBound(b) to UBound(b)
  Debug.Print b(i)
Next

'convert back:

strSomeOtherString = b

Msgbox strSomeOtherString
 
Upvote 0
Might also be worth taking a look into regular expressions.:)

Yeah, I've tried. RegEx makes my brain hurt. :/

Mind you seems like a strange request, to me anyway.:)

Basically, there's a sheet in Excel that we have to use at work. One part of it consists of 30, very narrow columns, and we have to put a letter into each cell (this sheet is printed and used as backup for journal entries - the 30 character limit is because that is the limit in JD Edwards for journal entry descriptions). A redesign is out of the question, so I'm working on a workaround, where you click a button which opens a userform/input box (haven't decided yet) with a single text box, you enter your description there, and then each character goes into the respective cell. Much easier than keying the description in letter by letter.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,612
Members
449,238
Latest member
wcbyers

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top