let's say I have string = "hfeiui37u298,39843u82,3084983"
and I want to get the the string between the commas...
can I do that in a single line of code?
Use the following function (I place it in a global subs module) to return a specific value from a comma separated string. Just pass the string of values and the number of the item you wish to return. If you pass an index that does not correspond to a position in the string, a blank string is returned. This can easily be modified to look for a separator other than a comma, and to return something else when the index is out of bounds.
Public Function GetCsvValue(strLine As String, Index As Integer) As String
Dim intAbsIndex As Integer ' Actual array position of index (1 less than # passed)
Dim strElements() As String ' Array to hold elements extracted from string
Dim strRetVal As String ' Value to be returned
intAbsIndex = Index - 1
strElements = Split(strLine, "," ' Change "," to change separator
' Check to see if index value is valid for this array
If (intAbsIndex < LBound(strElements)) Or (intAbsIndex > UBound(strElements)) Then
strRetVal = "" ' Value returned when index is out of bounds
Else
strRetVal = strElements(intAbsIndex)
End If
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.