I need to convert a string into an array of number in Excel 97


Posted by bill on September 22, 2000 9:07 AM

I have a string for example ( "1,2,3,4,5")
I want to turn this into an array that would be like this

Array name: numbers()
numbers(1) = 1
numbers(2) = 2
numbers(3) = 3

Any help please ??



Posted by Ivan Moala on September 22, 2000 1:23 PM

Try this function from

'Purpose : Converts a String to a 1 Dimensional Array.
'Inputs : ThisString. The string to convert.
' Delimiter. The item delimiter for the string.
'Outputs : ResultArray. The resulting array.
'Author : Andrew Baker
'Date : 25/03/2000
'Notes : Written as a Sub routine for speed.

Sub StringToArray(ByVal ThisString As String, ResultArray() As String, Optional Delimiter As String = ";", Optional TrimSpaces As Boolean)
Dim PosDelimiter As Long, LenString As Long, nItems As Long, OldPos As Long, LenDel As Long

LenString = Len(ThisString)
LenDel = Len(Delimiter)

If LenString Then
If Right$(ThisString, 1) <> Delimiter Then
ThisString = ThisString & Delimiter
End If
ReDim ResultArray(1 To LenString)
OldPos = 1
PosDelimiter = InStr(1, ThisString, Delimiter)

If TrimSpaces Then
Do While PosDelimiter
nItems = nItems + 1
ResultArray(nItems) = Trim$(Mid$(ThisString, OldPos, PosDelimiter - OldPos))
OldPos = PosDelimiter + LenDel
PosDelimiter = InStr(OldPos, ThisString, Delimiter)
Loop
Else
Do While PosDelimiter
nItems = nItems + 1
ResultArray(nItems) = Mid$(ThisString, OldPos, PosDelimiter - OldPos)
OldPos = PosDelimiter + LenDel
PosDelimiter = InStr(OldPos, ThisString, Delimiter)
Loop
End If
ReDim Preserve ResultArray(1 To nItems)
Else
Erase ResultArray
End If
End Sub


HTH


Ivan