maggie
08-01-2011, 01:57 PM
I have created the following 'search engine' for an Excel file. This search tool looks for keywords then spits the results out on a "Home" sheet. This works well.
My Question is:
Say I want to search the keyword "Cat". However as the keyword "Cat" also means "Kitten", I would like the engine to return all values that say Cat and Kitten. As well as when "Kitten" is searched, I would like all Kitten and Cat values to be returned. Any Ideas on how to write this into my program. There are only two instances where this occurs so I could write it in per instance.
Ex. Cat also could be searched as Kitten
Dog could also be searched as Puppy. (Only two instances)
Thank you in advance ~ Maggie
Program Below
Private Sub cmdSearch_Click()
Worksheets("Home").Select
Range("A3:G65536").Select
Selection.Clear
Selection.RowHeight = StandardHeight
Dim ws As Worksheet, myvar As String, val1 As Range
Dim val2 As Range, tmp As Range, cnt As Integer
cnt = 0
myvar = InputBox("Please Enter a Keyword:")
If myvar = "" Then Exit Sub
For Each ws In ThisWorkbook.Worksheets
Set val1 = ws.Cells.Find(What:=myvar, LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=False)
If Not val1 Is Nothing Then
cnt = cnt + 1
Application.Goto val1
ActiveCell.EntireRow.Select
Selection.Copy
Worksheets("Home").Select
Range("A1").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Set tmp = val1
again:
Set val2 = ws.Cells.FindNext(After:=val1)
If val1.Address <> val2.Address And _
tmp.Address <> val2.Address Then
Application.Goto val2
ActiveCell.EntireRow.Select
Selection.Copy
Worksheets("Home").Select
Range("A2").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Set val1 = val2
GoTo again
End If
End If
Next ws
If cnt = 0 Then MsgBox "No Matches Found"
End Sub
My Question is:
Say I want to search the keyword "Cat". However as the keyword "Cat" also means "Kitten", I would like the engine to return all values that say Cat and Kitten. As well as when "Kitten" is searched, I would like all Kitten and Cat values to be returned. Any Ideas on how to write this into my program. There are only two instances where this occurs so I could write it in per instance.
Ex. Cat also could be searched as Kitten
Dog could also be searched as Puppy. (Only two instances)
Thank you in advance ~ Maggie
Program Below
Private Sub cmdSearch_Click()
Worksheets("Home").Select
Range("A3:G65536").Select
Selection.Clear
Selection.RowHeight = StandardHeight
Dim ws As Worksheet, myvar As String, val1 As Range
Dim val2 As Range, tmp As Range, cnt As Integer
cnt = 0
myvar = InputBox("Please Enter a Keyword:")
If myvar = "" Then Exit Sub
For Each ws In ThisWorkbook.Worksheets
Set val1 = ws.Cells.Find(What:=myvar, LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=False)
If Not val1 Is Nothing Then
cnt = cnt + 1
Application.Goto val1
ActiveCell.EntireRow.Select
Selection.Copy
Worksheets("Home").Select
Range("A1").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Set tmp = val1
again:
Set val2 = ws.Cells.FindNext(After:=val1)
If val1.Address <> val2.Address And _
tmp.Address <> val2.Address Then
Application.Goto val2
ActiveCell.EntireRow.Select
Selection.Copy
Worksheets("Home").Select
Range("A2").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Set val1 = val2
GoTo again
End If
End If
Next ws
If cnt = 0 Then MsgBox "No Matches Found"
End Sub