Debug.Print StringFirstFirstChars(sSearch,vbNewline,vbCr,vbLf)
The code below is useful when you want to find the position of first character from a list of characters within a string. This would typically be used when looking for carriage returns, eg:
Debug.Print StringFirstFirstChars(sSearch,vbNewline,vbCr,vbLf)
'Purpose : Finds the position of the first character found in a string from a list of characters.
'Inputs : sSearchIn The string to search for the first instance of a character from the character list
' avChars The list of characters to search through "sSearchIn" for
'Outputs : Returns the position of first instance of a character found in a string from the list of supplied characters,
' or zero if none of the characters are found
'Example : If your wanted to find a line feed, you might use:
' Debug.Print StringFindFirstChar("ABCDEF","B","E","F")
' 'This would return 2, i.e. the position of character "B"
'Revisions :
Function StringFindFirstChar(sSearchIn As String, ParamArray avChars() As Variant) As Long
Dim lPosChar As Long, lPosTest As Long, vChar As Variant
On Error GoTo ErrFailed
If Len(sSearchIn) Then
For Each vChar In avChars
lPosTest = InStr(1, sSearchIn, CStr(vChar))
If lPosTest Then
If lPosChar = 0 Then
lPosChar = lPosTest
ElseIf lPosTest < lPosChar Then
lPosChar = lPosTest
End If
End If
Next
End If
StringFindFirstChar = lPosChar
Exit Function
ErrFailed:
Debug.Print Err.Description
Debug.Assert False
'Try the next line
Resume Next
End Function
No comments:
Post a Comment