Retrieving Lists of Strings in Statistica Visual Basic

The subroutine GetDelimitedString can be used to retrieve substrings from a string which is sectioned off by delimited characters. Simply enter the string which contains the concatenated strings as the first parameter, the section number you wish to retrieve, the target string you wish to store the retrieved string into, and the character that is being used as the delimiter; if the fourth parameter is left blank then the pipe ("|") symbol will be used as the delimiter. Simply copy and paste the following into your macro to utilize GetDelimitedString:

Sub GetDelimitedString _

(Source As String,Section As Integer, _
 ByRef Target As String, _
 Optional Character As String = "|")
 Dim Index As Integer
 Dim Length As Integer
 Dim TrimSize As Integer
 Dim SectionValidate As Integer
 Dim StringValidate As String

 'section must be greater than zero
 If Section < 1 Then

MsgBox "Section must be greater than zero."
 Exit Sub

End If

 'section cannot be greater than the number of sections 'in the source string
 Index = 1
 StringValidate = Source
 While Index > 0 'just use this to start the loop

Length = Len(StringValidate)
 'search for a "|" in the string
 Index = InStr(1,StringValidate,Character)
 TrimSize = Length - Index
 'slice off the "|" and everything in front of it
 StringValidate = Right(StringValidate,TrimSize)
 'keep track of the number of sections
 'separated by a "|"
 SectionValidate = SectionValidate + 1

Wend

 If Section > SectionValidate Then

Dim ErrorMsg As String
 ErrorMsg = "There are only " & _
 SectionValidate & " sections in this string."
 Call MsgBox(ErrorMsg,vbExclamation,"Invalid Section")
 Exit Sub

End If

 'trim the string to the section specified
 For i = 1 To (Section - 1)

Length = Len(Source)
 'search for a "|" in the string
 Index = InStr(1,Source,Character)
 TrimSize = Length - Index
 'slice off the "|" and everything in front of it
 Source = Right(Source,TrimSize)

Next i
 'if there any "|" characters left then
 'trim off everything after the first one to the left
 Index = InStr(1,Source,Character)
 If Index Then

Source = Left(Source,(Index -1))

End If
 'write over the target string with
 'the desire string section
 Target = Source

End Sub