VB and VBA Users Source Code: Finding the position of the first character from a list of characters
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Finding the position of the first character from a list of characters
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, April 21, 2003
Hits:
1756
Category:
Visual Basic General
Article:
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) 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 'Author : Andrew Baker 'Date : 15/01/2001 22:34 '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
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder