Code amended to reflect your comments
(1)
Can this be modified such as they key word searches are not for the EXACT words. Even if it is "gift" or "gifts" it should be considered.
Have replaced "EQUAL TO" with "LIKE" plus the wildcard "*" which means that any word beginning with the keyword will be matched.
If keyword is tax, matches could be tax, taxed, taxes, taxi etc
If keyword is taxes, only taxes would be matched from above list because it is the only one beginning with those 5 letters.
So the code becomes:
If wsM.Cells(r, c) Like arrayK(w) & "*" Then
(2)
Also, the result2 should be amended for each search. If I run the macro again, the results should be after the last row of the result2 sheet.
Previous results are now retained and, by using a new variable LastRowR to set the starting point for variable z, results are now added after the last row of "result2"
(3)
This is the sample sheet I have attached with few columns and rows. Will this work for the dynamic rows and column.
The rows are dynamic - so we do not need to amend anything
The columns are not dynamic.
The code is now amended to work for up to 12 columns (A to L). To amend it to reflect the number of columns in your worksheet, make changes as follows - replace 12 with the number of columns in your worksheet:
Amend in 3 lines the "L" to match your last column and amend the array to reflect the number of columns
wsR.Range("A2:L" & z - 1).RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), Header:=xlNo
wsM.Range("A1:L1").Copy
wsR.Range("A1:L1").PasteSpecial xlPasteValues
The spreadsheet with my test data is attached.
Amended macro:
Sub Search_Key_Words()
'declare and set variables
Dim LastRowK As Long, LastRowM As Long, LastRowR As Long
Dim r As Integer, c As Integer, w As Integer, x As Integer, y As Integer, z As Integer
Dim arrayK() As Variant
Dim wsM As Worksheet, wsR As Worksheet, wsK As Worksheet
Set wsM = Sheets("master")
Set wsR = Sheets("result2")
Set wsK = Sheets("Keyword")
'determine last row
LastRowK = wsK.Range("A1").End(xlDown).Row
LastRowM = wsM.Range("A1").End(xlDown).Row
If wsR.Range("A2") = "" Then
LastRowR = 1
Else
LastRowR = wsR.Range("A1").End(xlDown).Row
End If
'set dimensions of array
ReDim arrayK(LastRowK - 2)
'place keywords in array
For x = 0 To LastRowK - 2
arrayK(x) = wsK.Cells(x + 2, 1)
Next x
'create header row and set first row for data in results sheet
wsM.Range("A1:L1").Copy
wsR.Range("A1:L1").PasteSpecial xlPasteValues
z = LastRowR + 1
'run through columns E to H and check for values to match keywords
For w = 0 To LastRowK - 2
For r = 2 To LastRowM
For c = 5 To 8
If wsM.Cells(r, c) Like arrayK(w) & "*" Then
For y = 1 To 12
wsR.Cells(z, y) = wsM.Cells(r, y)
Next y
z = z + 1 'adds 1 to row number in results sheet
Else
'do nothing
End If
Next c
Next r
Next w
'remove duplicate entries - could happen if keyword matches appear in more than one column
wsR.Range("A2:L" & z - 1).RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), Header:=xlNo
End Sub