Visual Basic Programming Code Examples Visual Basic > Other Code Examples Instr with wildcards Instr with wildcards The following routine is the same as the instr routine, but it accepts the "*" wildcard: 'Purpose : Same as Instr() but accepts the "*" wildcard. 'Inputs : sSearchText The text to search in ' sForText The text to search for ' [lStart] The position of the first character to search on ' [bCompareText] If True, search is case insensitive 'Outputs : Returns the position of the match or zero if no match was found Function Instr2(sSearchText As String, sForText As String, Optional ByVal lStart As Long = 1, Optional bCompareText As Boolean = False) As Long Dim lWildCardStartPos As Long, lWildCardPos As Long, lWildCardEndPos As Long Dim sSubtext As String, lCompare As Long 'Set variables lWildCardStartPos = lStart If bCompareText Then lCompare = vbTextCompare Else lCompare = vbBinaryCompare End If Do 'Search for wildcard lWildCardPos = InStr(lWildCardPos + 1, sForText, "*") If lWildCardPos Then lWildCardEndPos = lWildCardPos - 1 Else lWildCardEndPos = Len(sForText) End If 'Search input text sSubtext = Mid$(sForText, lWildCardStartPos, lWildCardEndPos - lWildCardStartPos + 1) lStart = InStr(lStart, sSearchText, sSubtext, lCompare) 'Check exit conditions If lStart = 0 Then 'Clear value and exit do Instr2 = 0 Exit Do ElseIf Instr2 = 0 Then 'Store value Instr2 = lStart End If If lWildCardPos = 0 Then Exit Do End If lWildCardStartPos = lWildCardPos + 1 Loop End Function