How to do multiple lookups with VBA

Bassie

Board Regular
Joined
Jan 13, 2022
Messages
66
Office Version
  1. 2019
Platform
  1. Windows
Hey,

I have a list of products that I want to lookup against a big database of products. The difficult part is that one product can have multiple matches in the database and I need to have all matches come up in the results (unlike with vlookup). Also each product has multiple criteria that I want to copy down to the results page.

I made a XL2BB document with hopefully a little bit of a more clear explanation. In reality the database has around 100k rows of data.

Book1
ABCDEFGHIJKLMNO
1Search theseIn this tableColourSizeAvailabilityPriceResult:ColourSizeAvailabilityPrice
211Green10Yes61Green10Yes6
323Yellow12No41Green8No4
431Green8No41Lightblue10No1
542Blue20Yes22Blue20Yes2
651Lightblue10No13Yellow12No4
764Yellow12no54Yellow12no5
874Brown10Yes114Brown10Yes11
988Red22No126Purple16Yes2
107Pink12No157Pink12No15
116Purple16Yes27Green13Yes21
127Green13Yes218Red22No12
13etc
14etc
15etc
16
17
18
19
Sheet1




Greetings,
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Are the search criteria (column A), the search table and result table all in the same sheet? If not, please post XL2BB screenshots of each sheet.
 
Upvote 0
Hey,

Thanks for the tip. They are indeed not in the same sheet, I added a more relevant XL2BB screenshot below:

Search page
Mockup VBA lookup question.xlsx
ABCD
1ProductSearch these attributes
2X1
3Y2
4A3
5D4
6F5
7G6
8H7
9J8
10
11
Search this


Database:
Mockup VBA lookup question.xlsx
ABCDE
1In this tableColourSizeAvailabilityPrice
21Green10Yes6
33Yellow12No4
41Green8No4
52Blue20Yes2
61Lightblue10No1
74Yellow12no5
84Brown10Yes11
98Red22No12
107Pink12No15
116Purple16Yes2
127Green13Yes21
13etc
14etc
15etc
Database


Results

Mockup VBA lookup question.xlsx
ABCDEFG
1ProductSearch attributeColourSizeAvailabilityPrice
2X1Green10Yes6
3X1Green8No4
4X1Lightblue10No1
5Y2Blue20Yes2
6A3Yellow12No4
7D4Yellow12no5
8D4Brown10Yes11
9G6Purple16Yes2
10H7Pink12No15
11H7Green13Yes21
12J8Red22No12
13
Results
 
Upvote 0
Does the Report sheet contain only the search criteria? Basically I need to know how many sheets we are dealing with and I need to see what each sheet looks like.
 
Upvote 0
this code pulls your search from sheet called FIND (like you have in col.A)
then searches the data in sheet DATA,
then posts results in new sheets.
modify to fit your data
Code:
'---------------------
Sub FindMyData()
'---------------------
Dim iRows As Long, iFldNum As Long, iResultOFF As Long
Dim vTxt
Dim sResultCol As String
Const kResultHdr = "Results"
Const kFOUND = "found"
Dim colItems As New Collection
Dim i As Integer
Dim bIsFound As Boolean
  'load the legal search values
Sheets("find").Activate
Range("A1").Select
While ActiveCell.Value <> ""
   colItems.Add ActiveCell.Value
   ActiveCell.Offset(1, 0).Select 'next row
Wend
  'add a result column
Sheets("data").Activate
Range("A1").Select
Selection.End(xlToRight).Select
If InStr(ActiveCell.Value, kResultHdr) = 0 Then
   ActiveCell.Offset(0, 1).Select
   ActiveCell.Value = kResultHdr
End If
iFldNum = ActiveCell.Column
iResultOFF = iFldNum - Range("A1").Column
sResultCol = iFldNum & ":" & iFldNum
sResultCol = getMyColLtr()
  'get #rows
Range("A1").Select
iRows = ActiveSheet.UsedRange.Rows.Count
For i = 1 To colItems.Count
     bIsFound = False
 
      'clear results col.
    Columns(iFldNum).ClearContents
    Range(sResultCol & "1").Value = kResultHdr
   
    'MsgBox iRows
    Range("A2").Select
    While ActiveCell.Row <= iRows
       vTxt = ActiveCell.Offset(0, 0).Value
         
          If Val(vTxt) = Val(colItems(i)) Then
            ActiveCell.Offset(0, iResultOFF).Value = kFOUND
            bIsFound = True
          End If
       
       ActiveCell.Offset(1, 0).Select 'next row
    Wend
        If bIsFound Then
             'filter results
            ActiveSheet.Range("A1").AutoFilter Field:=iFldNum, Criteria1:=kFOUND
       
              'copy the results
                Range("A1").Select
                ActiveSheet.UsedRange.Select
                   
                Selection.Copy
                Sheets.Add After:=ActiveSheet
                ActiveSheet.Paste
                Application.CutCopyMode = False
                ActiveSheet.Name = "val " & colItems(i)
                Sheets("DATA").Select
                 Selection.AutoFilter
                Range("A1").Select
        End If
Next  'item
Set colItems = Nothing

End Sub
'---------------------
Public Function getMyColLtr()
'---------------------
Dim vRet
Dim i As Integer
vRet = Mid(ActiveCell.Address, 2)
i = InStr(vRet, "$")
If i > 0 Then vRet = Left(vRet, i - 1)
getMyColLtr = vRet
End Function
 
Upvote 0

Forum statistics

Threads
1,214,835
Messages
6,121,880
Members
449,057
Latest member
Moo4247

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