I have this textbox named txtnum in which I have to enter a 15 digit number and allocate it to variable num. I want to split the number into individual characters so that j can carry out calculations on each. Something like: product= arraynum[2]*2 . how do I split the string in the text box into array characters?
Asked
Active
Viewed 4,316 times
1
-
https://stackoverflow.com/questions/13195583/split-string-into-array-of-characters – Jim Hewitt Aug 10 '17 at 03:27
-
1Possible duplicate of [Split string into array of characters?](https://stackoverflow.com/questions/13195583/split-string-into-array-of-characters) – C-Pound Guru Aug 10 '17 at 13:59
2 Answers
4
Nothing is built-in (as far as I know), but it I easy enough to write a function which takes a string and returns an array of characters:
Function ToArray(s As String) As Variant
Dim A As Variant
Dim i As Long, n As Long
n = Len(s)
ReDim A(0 To n - 1)
For i = 0 To n - 1
A(i) = Mid(s, i + 1, 1)
Next i
ToArray = A
End Function
Having done this, there is little actual gain from using a function like this as opposed to simply using Mid()
.

John Coleman
- 51,337
- 7
- 54
- 119
2
Here is another option:
Dim s As Variant
s = "012345678901234"
s = StrConv(s, vbUnicode)
s = Split(s, vbNullChar)
s
will contain an array of characters.

Brian M Stafford
- 8,483
- 2
- 16
- 25
-
6This will break if the string contains characters outside of the "commonly used in English" range. It works for "only digits" but even then it's still an ugly hack that should not be used. – Tomalak Aug 09 '17 at 21:01