By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
Function Field(InputStr As String, DelimChr As String, PosNo As Integer) 'The DelimChr must be a single character or else function will return an error MaxLen = Len(InputStr) ErrMsg = "" If PosNo < 1 Then ErrMsg = "Starting position < 1" Elseif DelimChr = "" Then ErrMsg = "Delimeter is null" Elseif Len(DelimChr) > 1 Then ErrMsg = "Delimeter is > 1 character" Elseif MaxLen < 1 Then Field = "" Exit Function 'No input string passed to function - just return an empty string - not considered an error End If If ErrMsg <> "" Then Msgbox "Error: Invalid parameter!" + Chr(13) + Chr(13) + ErrMsg,16,"Field Function" Field = "" Exit Function End If DelimCnt = 0 'Keeps track of how many delimiters were found StartSrchPos = 1 'Initialize where to start looking in the string StartDelimPos = 0 'Will hold the physical postion where the desired delimiter was found EndDelimPos = 0 'Will hold the physical position where the next delimiter was found For J = 1 To PosNo DelimPos = Instr(StartSrchPos,InputStr,DelimChr) If DelimPos > 0 Then 'Delimiter found in the sub-string DelimCnt = DelimCnt + 1 'Keep track of what number of delimiter this is If DelimCnt = PosNo - 1 Then StartDelimPos = DelimPos 'Keep going - not the right delimiter position Elseif DelimCnt = PosNo Then EndDelimPos = DelimPos Exit For 'Exit loop - the right delimiter position has been found End If StartSrchPos = DelimPos + 1 'The next search will begin just after the last delimiter was found Else Exit For 'Exit loop - no more delimiters found End If Next J If StartDelimPos > 0 And EndDelimPos > 0 Then If StartDelimPos+1 = EndDelimPos Then Field = "" Exit Function 'There is no gap between the two delimiter positions - return nothing End If End If If StartDelimPos > 0 Or EndDelimPos > 0 Then If EndDelimPos > 0 Then Field = Mid$(InputStr,StartDelimPos+1,EndDelimPos-(StartDelimPos+1)) 'Return those bytes between the 2 delimiter positions Else Field = Mid$(InputStr,StartDelimPos+1,MaxLen) 'No subsequent delimiter was found - return the remainder of the string End If Else 'If the input string doesn't contain a delimiter and the 1st occurrance was requested, just return the entire string else return nothing If PosNo = 1 And DelimCnt = 0 Then Field = InputStr Else Field = "" End If End Function