#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Search function

I have been trying to create a search bar in Excel. I want to be able
to enter some letters into a cell, then have those letters be matched
with the closest resemblance in a range of text cells, and return the
number in the cell next to it. (I tried the LOOKUP functions, but if
the query is not exactly the same as the text in the array, it returns
the wrong value.)

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,069
Default Search function

Try the following subroutine & function. It finds the closest match within a
range of cells to the value in one particular cell. It returns (via MsgBox)
the address of the cell which is the closest match (or exact match, if it
finds one.)

Sub ClosestMatch()
Dim msg1 As String, R1 As Range, R2 As Range
Dim Closest As Range, EquivPct As Double
Dim CurrRecPct As Double
On Error GoTo CMerr1
EquivPct# = 0
'R1 is cell with text to match
Set R1 = Range("A1")
'R2 is current cell in selected range to search
Range("B5:M50").Select
For Each R2 In Selection
If R1.Value = R2.Value Then
MsgBox "Found exact match in cell " _
& R2.Address
GoTo Cleanup1
End If
CurrRecPct# = Equivalence(R1, R2)
If CurrRecPct# EquivPct# Then
EquivPct# = CurrRecPct#
Set Closest = R2
End If
Next R2
MsgBox "Closest match was cell " & _
Closest.Address
Cleanup1:
Set R1 = Nothing
Set Closest = Nothing
Exit Sub
CMerr1:
If Err.Number < 0 Then
msg1$ = "Error # " & Str(Err.Number) & _
" was generated by " & Err.Source & _
Chr(13) & Err.Description
MsgBox msg1$, , "ClosestMatch", _
Err.HelpFile, Err.HelpContext
End If
GoTo Cleanup1
End Sub

Public Function Equivalence(Rng1 As Range, _
rng2 As Range) As Double
Dim MtchTbl(100, 100)
Dim MyMax As Double, ThisMax As Double
Dim i As Integer, j As Integer
Dim ii As Integer, jj As Integer
Dim st1 As String, st2 As String
If (Rng1.Count 1) Or (rng2.Count 1) Then
MsgBox "Arguments for Equivalence function " & _
"must be individual cells", vbExclamation, _
"Equivalence error"
Equivalence = -1
End If
st1$ = Trim(LCase(Rng1.Value))
st2$ = Trim(LCase(rng2.Value))
MyMax# = 0
For i% = Len(st1$) To 1 Step -1
For j% = Len(st2$) To 1 Step -1
If Mid(st1$, i%, 1) = Mid(st2$, j%, 1) Then
ThisMax# = 0
For ii% = (i% + 1) To Len(st1$)
For jj% = (j% + 1) To Len(st2$)
If MtchTbl(ii%, jj%) ThisMax# Then
ThisMax# = MtchTbl(ii%, jj%)
End If
Next jj%
Next ii%
MtchTbl(i%, j%) = ThisMax# + 1
If (ThisMax# + 1) ThisMax# Then
MyMax# = ThisMax# + 1
End If
End If
Next j%
Next i%
Equivalence# = MyMax# / ((Len(st1$) + Len(st2$)) / 2)
End Function

Hope this helps,

Hutch

"Haim" wrote:

I have been trying to create a search bar in Excel. I want to be able
to enter some letters into a cell, then have those letters be matched
with the closest resemblance in a range of text cells, and return the
number in the cell next to it. (I tried the LOOKUP functions, but if
the query is not exactly the same as the text in the array, it returns
the wrong value.)


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VLookup function to search an entire workbook liseladele Excel Worksheet Functions 0 November 10th 05 12:35 AM
Function to search a row of data. cwade23 Excel Worksheet Functions 6 June 8th 05 12:53 AM
a search function jacko Excel Worksheet Functions 1 June 1st 05 12:51 PM
Search function nc Excel Discussion (Misc queries) 7 May 13th 05 03:08 PM
Search function using commas Kylie Excel Worksheet Functions 1 February 2nd 05 02:46 AM


All times are GMT +1. The time now is 02:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"