Public Function InStrLike(Optional Start, Optional String1, Optional String2, Optional intCompareMethod As VbCompareMethod = vbTextCompare) As Variant
On Error Goto err_InStrLike Dim intPos As Integer Dim intLength As Integer Dim strBuffer As String Dim blnFound As Boolean Dim varReturn As Variant If Not IsNumeric(Start) And IsMissing(String2) Then String2 = String1 String1 = Start Start = 1 End If If IsNull(String1) Or IsNull(String2) Then varReturn = Null Goto exit_InStrLike End If If Left(String2, 1) = "*" Then err.Raise vbObjectError + 2600, "InStrLike", "Comparison mask cannot start With '*' since a start position cannot be determined." Exit Function End If For intPos = Start To Len(String1) - Len(String2) + 1 If InStr(1, String2, "*", vbTextCompare) Then For intLength = 1 To Len(String1) - intPos + 1 strBuffer = Mid(String1, intPos, intLength) If strBuffer Like String2 Then blnFound = True Goto done End If Next intLength Else strBuffer = Mid(String1, intPos, Len(String2)) If strBuffer Like String2 Then blnFound = True Goto done End If End If Next intPos done: If blnFound = False Then varReturn = 0 Else varReturn = intPos End If exit_InStrLike: InStrLike = varReturn Exit Function err_InStrLike: Select Case err.Number Case Else varReturn = Null MsgBox err.Description, vbCritical, "Error #" & err.Number & "(InStrLike)" Goto exit_InStrLike End Select End Function Example: InStrLike("Test String 123abc45 Stuff","###*##") returns 13, because 123abc45 matches the mask and it starts at character 13. Hope this is useful to somebody. Inputs: Start=Position to start searching, optional String1=String To search String2=Mask To search For intCompareMethod=vbCompareMethod To use, optional |