Need to return multiple values based upon multiple criteria

klop

New Member
Joined
Feb 12, 2011
Messages
2
I'm not sure if Excel or Access is the best tool.

I am seeking to search for roughly 1,000 unique search strings amongst 100,000 customers and identify all string matches. The customer data set consists of customer ID (column A) and customer name (column B). I would like to return all customer ID's and customer names that match any of the 1,000 search strings in another tab or in a dedicated query (if performed in access). For clarification if necessary, I would like the match to be a contains match, not necessarily an exact match.

I assume this could be done in VBA by looking up every line? Does anyone have similar code that could be applied?

Thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi,

You can try this on some test data to see if it's the sort of thing you want.

Basic data, i.e customer names and ID's on sheet1, cols 1 and 2, and the 1000 search strings down Column 1 on sheet2.

Try it on test or sample data first.
Code:
Sub matchesandstuff()
Dim a As Range, b, c(), f, x, y
b = Sheets("sheet2").Range("A1").CurrentRegion
Sheets("sheet1").Activate
n = Range("A:B").Find("*", LookIn:=xlValues, searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Row
Set a = Range("A1").Resize(n, 2)
ReDim c(1 To n, 1 To 1)
For Each e In b
    Set f = a.Find(e, lookat:=xlPart, MatchCase:=True)
    If Not f Is Nothing Then
        x = f.Row
        c(x, 1) = 1
        Do
            Set f = a.FindNext(f)
            c(f.Row, 1) = 1
        Loop While f.Row <> x
    End If
Next e
Range("C:C").Insert
Range("C1").Resize(n) = c
a.Resize(, 3).Sort [c1], 1
y = Range("C1").End(4).Row
a(y + 1, 1).Resize(n - y, 2).Clear
Range("C:C").Delete
End Sub
 
Upvote 0
Thanks - it appears to work. However, the values were returned on sheet 1 and replaced the original customer name and id. Is it possible to place the search results in another tab?

Thanks so much - this is a tremendous time saver for me!
 
Upvote 0
How about this modification.

Should leave Sheet1 unchanged and lists results on Sheet3.
Code:
Sub matchesandstuff2()
Dim a As Range, b, c(), f, x, y
Application.ScreenUpdating = False
b = Sheets("sheet2").Range("A1").CurrentRegion
Sheets("sheet1").Activate
n = Range("A:B").Find("*", LookIn:=xlValues, searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Row
ReDim c(1 To n, 1 To 1)
Sheets("sheet3").Activate
Sheets("sheet1").Range("A1").Resize(n, 2).Copy [a1]
Set a = Range("A1").Resize(n, 2)
For Each e In b
    Set f = a.Find(e, lookat:=xlPart, MatchCase:=True)
    If Not f Is Nothing Then
        x = f.Row
        c(x, 1) = 1
        Do
            Set f = a.FindNext(f)
            c(f.Row, 1) = 1
        Loop While f.Row <> x
    End If
Next e
Range("C:C").Insert
Range("C1").Resize(n) = c
a.Resize(, 3).Sort [c1], 1
y = Range("C1").End(4).Row
a(y + 1, 1).Resize(n - y, 2).Clear
Range("C:C").Delete
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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