Guest

Untitled

By: a guest on Feb 20th, 2012  |  syntax: VisualBasic  |  size: 2.30 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. Function fuzzy_search(searchStr As String, lookupFields As range) As Variant
  2. Dim r As Object
  3. Dim r2 As Object
  4. Dim i As Integer
  5. Dim searchRange As range
  6. Dim patternStr As String
  7. Dim curPatternStr As String
  8.  
  9. fuzzy_search = "Í/Ä"
  10.  
  11. Set r = CreateObject("vbscript.regexp")
  12. With r
  13.       .Global = True
  14.       .IgnoreCase = True
  15.       .Pattern = "([a-zà-ÿ0-9_]+)"
  16. End With
  17.  
  18. Set r2 = CreateObject("vbscript.regexp")
  19. With r2
  20.       .Global = True
  21.       .IgnoreCase = True
  22.       .Pattern = "[^a-zà-ÿ0-9_]"
  23. End With
  24.  
  25. Set searchMatches = r.Execute(Trim(searchStr))
  26.  
  27. Dim m1 As Object
  28. Dim m2 As Object
  29.  
  30. Dim max As Integer
  31. Dim currentMax As range
  32. Dim tmp As String
  33. max = 0
  34. For Each searchRange In lookupFields
  35.     If LCase(r2.Replace(searchRange, "")) Like LCase(r2.Replace(searchStr, "")) Then
  36.         Set currentMax = searchRange
  37.         Exit For
  38.     End If
  39.    
  40.     tmp = ""
  41.     'MsgBox r2.Replace(searchRange, "*")
  42.    Set searchMatches2 = r.Execute(Trim(searchRange))
  43.     i = 0
  44.     For Each m2 In searchMatches2
  45.         'MsgBox Len(m2.SubMatches())
  46.        For Each m1 In searchMatches
  47.             If LCase(m1.Value) Like LCase(m2.Value) Then
  48.                 If Len(m2.Value) > 1 Then
  49.                     i = i + 1
  50.                     'MsgBox m1.Value & " " & i
  51.                End If
  52.             End If
  53.         Next
  54.     Next
  55.    
  56.     For Each m2 In searchMatches2
  57.         tmp = tmp + m2.Value
  58.         For Each m1 In searchMatches
  59.             If LCase(m1.Value) Like LCase(tmp) Then
  60.                 If Len(tmp) > 1 Then
  61.                     i = i + 1
  62.                     'MsgBox m1.Value & " " & i
  63.                End If
  64.             End If
  65.         Next
  66.     Next
  67.    
  68.     'For i = 98 To 9 Step -1
  69. 'do stuff eg: If Cells(i,"B").Value = "X" Then ....
  70. 'Next i
  71.    
  72.     'MsgBox i
  73.    
  74.     If i > max Then
  75.         'MsgBox i
  76.        max = i
  77.         Set currentMax = searchRange
  78.     End If
  79. Next
  80.  
  81. fuzzy_search = currentMax
  82. 'patternStr = r.Replace(Trim(searchStr), "*")
  83. 'curPatternStr = patternStr
  84. 'For i = 0 To Len(curPatternStr)
  85. '    curPatternStr = Left(curPatternStr, Len(curPatternStr) - i)
  86. '    MsgBox curPatternStr
  87. '    For Each searchRange In lookupFields
  88. '        If searchRange Like curPatternStr Then fuzzy_search = searchRange: Exit Function
  89. '    Next
  90. 'Next
  91.  
  92. End Function