PDA

View Full Version : fuzzy search macro



arnab0711
05-03-2012, 07:10 AM
Hi,
I have this file where I want to do a macro for fuzzy search,If I give a part of item description in the search box,and click on search,the file will search all item codes and items descriptions
in Summary Boq sheet,and give all item codes and item descriptions close to it in the output sheet

Tinbendr
05-03-2012, 08:28 AM
Try this.

arnab0711
05-03-2012, 09:50 AM
hi,
Its not opening with excel 2007

Tinbendr
05-03-2012, 10:02 AM
Hmm. I wrote it in 2007.

Here's the code. Copy it to the Main code page.

Private Sub CommandButton1_Click()
'http://vbaexpress.com/forum/showthread.php?t=42045
Dim Wb As Workbook
Dim WSrc As Worksheet
Dim WDest As Worksheet
Dim C As Range
Dim LastRow As Long
Dim CurRow As Long
Set Wb = ActiveWorkbook
Set WSrc = Wb.Worksheets("Summary Boq")
Set WDest = Wb.Worksheets("output")
'Find Lastrow of destination and clear
LastRow = WDest.Range("A65536").End(xlUp).Row
If LastRow > 1 Then
WDest.Range("A2:B" & LastRow).Clear
End If
'Set row number of destination
CurRow = 2
'Find last row of source
LastRow = WSrc.Range("C65536").End(xlUp).Row
'If searchbox is not empty...
If TextBox1 <> "" Then
'Set Range
With WSrc.Range("D2:D" & LastRow)
Set C = .Find(TextBox1.Value, LookIn:=xlValues, lookat:=xlPart)
If Not C Is Nothing Then
firstAddress = C.Address
Do
'Output to sheet
WDest.Range("B" & CurRow) = C
WDest.Range("A" & CurRow) = C.Offset(, -1)
'Increment row number
CurRow = CurRow + 1
'Find next occurence.
Set C = .FindNext(C)
Loop While Not C Is Nothing And C.Address <> firstAddress
End If
End With
'Clear searchbox
TextBox1 = ""
End If
WDest.Activate
End Sub