Hi,
I'm trying to set up a system that searches for a string within a range of cells and displays all the results. So far I have been able to display only exact matches, but I'd like to display any matches within a string.
I'm using this code which I found to search for text:
Sub SearchForString()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim SearchTerm As String
On Error GoTo Err_Execute
Sheets("sheet1").Range("a" & 1 & ":e" & 10).ClearContents
'Start search in row 4
LSearchRow = 15
'Start copying data to row 2 in Sheet2 (row counter variable)
LCopyToRow = 2
'Define Search term
SearchTerm = InputBox("What would you like to search for?")
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column E = SearchTerm, copy entire row to Sheet2
If Range("E" & CStr(LSearchRow)).Value = SearchTerm Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Sheet2 in next row
Sheets("Sheet1").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Sheet1").Select
End If
LSearchRow = LSearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
I've been trying different ways to check for text within the selected field, but can't seem to get it to work.
Any help would be appreciated, thanks
Bookmarks