Change code to search multiple terms across columns

earthandbody

New Member
Joined
Dec 10, 2014
Messages
4
Hello,

I have this code that I need modified to search more than one term at a time. I need it to search everything from columns A to P.

Here is the code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Dim sAddr As String
Dim LastRow1 As Long
LastRow1 = Sheets("Search").Range("A" & Rows.Count).End(xlUp).Row + 1
Dim LastRow2 As Long
LastRow2 = Sheets("2014-2019").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Sheets("Search").Range("A5:P" & LastRow1).ClearContents
Dim searchVal As Range
With Sheets("2014-2019").Range("A2:P" & LastRow2)
Set searchVal = .Find(Target, LookIn:=xlValues, LookAt:=xlPart)
If Not searchVal Is Nothing Then
sAddr = searchVal.Address
Do
searchVal.EntireRow.Copy
ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Set searchVal = .FindNext(searchVal)
Loop While searchVal.Address <> sAddr
sAddr = ""
End If
End With
Set searchVal = Nothing
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

Thanks in advance
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I need it to search everything from columns A to P.
Can you expand on this a little to define which rows in columns A:P you want to use as search criteria and then what do you want to do with the data once found? Please be specific with reference to rows and columns. It is difficult to code on generalities.
 
Last edited:
Upvote 0
The columns have data like delivery location, description, vendor, buyer name, etc. I may have pieces of each and want to enter what I have, and have it search and find the rows that contain all of the search string.

It already just shows all the matching rows underneath. I just need to be able to search multiple pieces of information.
 
Upvote 0
I'm thinking that maybe you want to use Advanced Filter. You can use several pieces of data as criteria and it will filter that out of the total range of data. Have you used Advanced Filter before?
 
Upvote 0
I'm thinking that maybe you want to use Advanced Filter. You can use several pieces of data as criteria and it will filter that out of the total range of data. Have you used Advanced Filter before?

I've used it but not much as I find it just easier to use the regular filter. I want in the search box because of how fast I need to acquire the information where it clunkier to reset the filters.
 
Upvote 0
I am still not clear on what and how you need to search. Can you post an example of the type of data ("pieces of each") that you would use as search criterea and what would be in the cells that are being searched. I need to understand this to know how to tell vba how to look for the critera.
 
Upvote 0
So some sample data on sheet two. On sheet one is the code and search box where the results show underneath but only with one search term like "binder". I need to just be able to enter say "binder & dwayne" and have matching results.
[TABLE="width: 1323"]
<tbody>[TR]
[TD]Supplier Item #[/TD]
[TD]Item Description[/TD]
[TD]UOM[/TD]
[TD] Unit Price[/TD]
[TD]Supplier[/TD]
[TD]Preparer[/TD]
[TD]Requester[/TD]
[TD]Deliver to Location[/TD]
[/TR]
[TR]
[TD]933015 XXL BLUE[/TD]
[TD]BINDER LINED XX LARGE BLUE[/TD]
[TD]BOX OF 12[/TD]
[TD] $ 289.30[/TD]
[TD]ALIMED INC[/TD]
[TD]DWAYNE JOHNSON[/TD]
[TD]DWAYNE JOHNSON[/TD]
[TD]3M3[/TD]
[/TR]
[TR]
[TD]4518-826[/TD]
[TD]TONER KONICA MINOLTA 2900/3900[/TD]
[TD]EACH[/TD]
[TD] $ 124.74[/TD]
[TD]KONICA MINOLTA[/TD]
[TD]DWAYNE JOHNSON[/TD]
[TD]DWAYNE JOHNSON[/TD]
[TD]3M3[/TD]
[/TR]
[TR]
[TD]4518-826[/TD]
[TD]TONER KONICA MINOLTA 2900/3900[/TD]
[TD]EACH[/TD]
[TD] $ 124.74[/TD]
[TD]KONICA MINOLTA[/TD]
[TD]DWAYNE JOHNSON[/TD]
[TD]DWAYNE JOHNSON[/TD]
[TD]3M3[/TD]
[/TR]
[TR]
[TD]LE2000[/TD]
[TD]LEECHES[/TD]
[TD]EACH[/TD]
[TD] $ 11.60[/TD]
[TD]LEECHES USA[/TD]
[TD]DWAYNE JOHNSON[/TD]
[TD]DWAYNE JOHNSON[/TD]
[TD]4K8[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Cross posted http://www.vbaexpress.com/forum/sho...-code-to-search-multiple-terms-across-columns

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
OK, that takes a little bit of complex coding because you are looking for two or more values in part of a string in two or more cells on the same row.
This code will accomodate two search criteria. I am not sure it is what you are looking for, but it is the best I can offer from what I understand about your objective.
Code:
Sub t()
Dim srch1 As String, srch2 As String, fn As Range, lc As Long, sh As Worksheet, i As Long, rw As Long, adr As String
Set sh = ActiveSheet
lc = sh.Cells.Find("*", , xlFormulas, xlPart, xlByColumns, xlPrevious).Column
srch1 = InputBox("Enter string to search", "SEARCH STRING ONE")
ans = MsgBox("Is there another string to search?", vbYesNo + vbQuestion, "2nd SEARCH OPTION")
    If ans = vbYes Then
        srch2 = InputBox("Enter string to search", "SEARCH STRING TWO")
    End If
    With sh
        Set fn = .UsedRange.Find(srch1, , xlValues, xlPart)
            If Not fn Is Nothing Then
                adr = fn.Address
                Do
                    For i = 1 To lc
                        If InStr(LCase(.Cells(fn.Row, i).Value), LCase(srch2)) > 0 And .Cells(fn.Row, i).Address <> adr Then
                            .Rows(fn.Row).Select
                            Exit Sub
                        End If
                    Next
                    Set fn = .UsedRange.FindNext(fn)
                Loop While fn.Address <> adr
            End If
    End With
MsgBox "Search criteria not found.", vbExclamation, "NOT FOUND"
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,787
Messages
6,174,561
Members
452,573
Latest member
Cpiet

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top