I often need to search Excel formulas for some special texts in a cell. The number of lines that I need to search is from 100,000 to 500,000, in rare cases - up to 1,000,000. To avoid long formulas, I wrote my own UDF to search for multiple text strings in a cell. The new formula is short for processing. I optimize the execution time of this formula as good as I can. It takes 11 to 12 seconds for 500,000 rows.
I made this formula in two ways: one uses IF-Statement (SuchenSIF), the other (SuchenSSELCASE) uses SELECT CASE statements. Booth formulas have the same speed. Can you give me some hint on how to get the best performance?
The syntax for this formula is:
SuchenSIF (search cell, search text 1, ... search text 6)
SuchenSSELCASE (search cell, search text 1, ... search text 6)
Public Function SuchenSIF(Zelle As Range, such1 As String, Optional such2 As String, Optional such3 As String, Optional such4 As String, Optional such5 As String, Optional such6 As String) As Integer
Application.Volatile
' this code, based on IF-statements need 11-12 seconds for 500.000 rows
' Start of IF-Section
'
ZelleWert = Zelle.Value
SuchenS = InStr(1, ZelleWert, such1, vbTextCompare)
If SuchenS > 0 Then Exit Function
SuchenS = InStr(1, ZelleWert, such2, vbTextCompare)
If SuchenS <> vbFalse Then Exit Function
If Len(such3) > 0 Then
SuchenS = InStr(1, ZelleWert, such3, vbTextCompare)
If SuchenS > 0 Then Exit Function
If Len(such4) > 0 Then
SuchenS = InStr(1, ZelleWert, such4, vbTextCompare)
If SuchenS > 0 Then Exit Function
If Len(such5) > 0 Then
SuchenS = InStr(1, ZelleWert, such5, vbTextCompare)
If SuchenS > 0 Then Exit Function
If Len(such6) > 0 Then
SuchenS = InStr(1, ZelleWert, such6, vbTextCompare)
If SuchenS > 0 Then Exit Function
End If
End If
End If
End If
'
' End of IF-Section
If SuchenS = 0 Then SuchenS = False
End Function
Public Function SuchenSSELCASE(Zelle As Range, such1 As String, Optional such2 As String, Optional such3 As String, Optional such4 As String, Optional such5 As String, Optional such6 As String) As Integer
Application.Volatile
' this code, based on SELECT-CASE-statements need 11-12 seconds for 500.000 rows
' Start of SELECT-CASE -Section
'
ZelleWert = Zelle.Value
SuchenS = InStr(1, ZelleWert, such1, vbTextCompare) * Len(such1)
Select Case SuchenS
Case 0
SuchenS = InStr(1, ZelleWert, such2, vbTextCompare) * Len(such2)
Select Case SuchenS
Case 0
SuchenS = InStr(1, ZelleWert, such3, vbTextCompare) * Len (such3)
Select Case SuchenS
Case 0
SuchenS = InStr(1, ZelleWert, such4, vbTextCompare) * Len(such4)
Select Case SuchenS
Case 0
SuchenS = InStr(1, ZelleWert, such5, vbTextCompare) * Len(such5)
Select Case SuchenS
Case 0
SuchenS = InStr(1, ZelleWert, such6, vbTextCompare) * Len(such6)
Select Case SuchenS
Case 0
Case Else
SuchenS = SuchenS / Len(such6)
Exit Function
End Select
Case Else
SuchenS = SuchenS / Len(such5)
Exit Function
End Select
Case Else
SuchenS = SuchenS / Len(such4)
Exit Function
End Select
Case Else
SuchenS = SuchenS / Len(such3)
Exit Function
End Select
Case Else
SuchenS = SuchenS / Len(such2)
Exit Function
End Select
Case Else
SuchenS = SuchenS / Len(such1)
Exit Function
End Select
'
' End of SELECT-CASE -Section
If SuchenS = 0 Then SuchenS = False
End Function