Public Function StrParse(Source As String, PChar As String) As Variant
'This Function take the 'Souce' parameter and a separater charater and _ creates an Array with each element containing a string ending where each _ 'PChar' was found. 'This Function works great for parsing command lines and/or Nested Path Strings. Dim pos As Integer, i As Integer Source = Left(Source, Len(Source) - 1) + PChar 'Make sure we get the last element in string. 'begin a loop Do 'find the first separating PChar pos% = InStr(Source, PChar) 'if there's one, then... If pos% Then ReDim Preserve TmpArray(i%) 'extract the string up to the PChar TmpArray(i%) = Trim(Left$(Source, pos% - 1)) 'and remove that from the Source string, 'so it won't be checked again Source = Mid$(Source, pos% + 1, Len(Source)) i% = i% + 1 End If Loop Until Source = "" Or Source = Chr(0) StrParse = TmpArray 'Return a New Populated Array. End Function Public Function BuildParseStr(vArray As Variant) As String 'This Function Takes Each Element in the Array Passed and Creates _ a Parseable String. Using the " ," as the Delimeter. Could be Changed to _ Accept any Character as the Delimeter. Dim i As Integer, BldStr As String If Not IsArray(vArray) Then 'If not an array then return zero length string. BuildParseStr = "" Exit Function End If For i = LBound(vArray) To UBound(vArray) 'Go thru each element in the array If VarType(vArray(i)) vbString Then ' Make sure all element are string type vArray(i) = CStr(vArray(i)) ' If Not Convert them to strings. End If If i = UBound(vArray) Then 'Keep from Appending last "," 'at the end of the final returned string BldStr = BldStr & vArray(i) Else BldStr = BldStr & vArray(i) & "," 'Build the String on the Fly. End If Next i BuildParseStr = BldStr ' Return Parseable String. End Function Here are two functions that work great for parsing and unparing strings.: Just copy the code and Place in a module : Enjoy! |