vba search and return all matching results from one single column

BoyBoy

New Member
Joined
Sep 25, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am looking for a script which can lookup and return all the value (all duplicates) in one column from multiple criteria.
My sheet 1 will look up the value in column E from sheet 2 the master table and return value (if exist) in column F, G & H.
I've only listed 8 storages in my example, but the total storage number could be up to 500, and the same apply to the master table in sheet 2.

Vehicle Model Inventory.xlsx
ABCDEFGH
1StorageVehicle ModelSerial No 1Serial No 2Serial No 3
2ANo 9
3BNo 9
4CNo 9
5DNo 9
6ENo 9
7FNo 9
8GNo 9
9HNo 9
Sheet1


Vehicle Model Inventory.xlsx
ABC
1StorageVehicle ModelSerial No
2ANo 9SX-123
3ANo 9SX-263
4ANo 4SX-123
5BNo 15SX-263
6BNo 9SX-123
7BNo 9SX-263
8BNo 2SX-123
9CNo 2SX-263
10CNo 2SX-123
11CNo 9SX-123
12CNo 4SX-123
13DNo 15SX-123
14DNo 15SX-263
15DNo 2SX-123
16DNo 2SX-578
17ENo 4SX-123
18ENo 9SX-123
19ENo 4SX-263
20FNo 9SX-263
21FNo 15SX-123
22FNo 4SX-123
23GNo 9SX-123
24GNo 9SX-263
25GNo 9SX-578
26HNo 2SX-123
27HNo 2SX-263
28HNo 2SX-578
Sheet2


A simple working vba code would be greatly appreciated.

BoyBoy
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about
VBA Code:
Sub BoyBoy()
   Dim Ary As Variant
   Dim r As Long
   Dim Dic As Object
   Dim Cl As Range
   
   Ary = Sheets("Sheet2").Range("A1").CurrentRegion.Value2
   Set Dic = CreateObject("scripting.dictionary")
   Dic.CompareMode = 1
   For r = 2 To UBound(Ary)
      If Not Dic.Exists(Ary(r, 1)) Then Dic.Add Ary(r, 1), CreateObject("scripting.dictionary")
      Dic(Ary(r, 1))(Ary(r, 2)) = Dic(Ary(r, 1))(Ary(r, 2)) & Ary(r, 3) & "|"
   Next r
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Cl.Offset(, 5).Value = Dic(Cl.Value)(Cl.Offset(, 4).Value)
      Next Cl
      .Range("F2", .Range("F" & Rows.Count).End(xlUp)).TextToColumns .Range("F2"), xlDelimited, , , 0, 0, 0, 0, 1, "|"
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub BoyBoy()
   Dim Ary As Variant
   Dim r As Long
   Dim Dic As Object
   Dim Cl As Range
  
   Ary = Sheets("Sheet2").Range("A1").CurrentRegion.Value2
   Set Dic = CreateObject("scripting.dictionary")
   Dic.CompareMode = 1
   For r = 2 To UBound(Ary)
      If Not Dic.Exists(Ary(r, 1)) Then Dic.Add Ary(r, 1), CreateObject("scripting.dictionary")
      Dic(Ary(r, 1))(Ary(r, 2)) = Dic(Ary(r, 1))(Ary(r, 2)) & Ary(r, 3) & "|"
   Next r
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Cl.Offset(, 5).Value = Dic(Cl.Value)(Cl.Offset(, 4).Value)
      Next Cl
      .Range("F2", .Range("F" & Rows.Count).End(xlUp)).TextToColumns .Range("F2"), xlDelimited, , , 0, 0, 0, 0, 1, "|"
   End With
End Sub
Thank you so much, this works like a charm in the example I've provided, however, I've got this error
1605469071607.png

when I tried it on a different scenario, like expanded the # of the storage in sheet1, but not all of the storages in sheet1 are listed in the master sheet (sheet2)

Vehicle Model Inventory.xlsm
ABCDEFGH
1StorageVehicle Model
2ANo 9
3BNo 9
4CNo 9
5DNo 9
6ENo 9
7FNo 9
8GNo 9
9HNo 9
10JNo 9
11KNo 9
12LNo 9
13MNo 9
14NNo 9
15ONo 9
16PNo 9
17QNo 9
18RNo 9
19SNo 9
20TNo 9
21UNo 9
22VNo 9
23WNo 9
24XNo 9
25YNo 9
26ZNo 9
Sheet1


Vehicle Model Inventory.xlsm
ABC
1StorageVehicle ModelSerial No
2KNo 15SX-123
3LNo 15SX-263
4MNo 4SX-123
5ONo 9SX-263
6ONo 9SX-123
7BNo 15SX-263
8BNo 2SX-123
9ZNo 2SX-263
10ZNo 2SX-123
11ZNo 15SX-123
12ZNo 4SX-123
13TNo 9SX-123
14TNo 9SX-263
15QNo 2SX-123
16QNo 2SX-578
17ENo 4SX-123
18ENo 15SX-123
19ENo 4SX-263
20FNo 9SX-263
21FNo 15SX-123
22FNo 4SX-123
23GNo 15SX-123
24GNo 9SX-263
25GNo 9SX-578
26HNo 2SX-123
27HNo 2SX-263
28HNo 2SX-578
29INo 9SX-578
30INo 2SX-123
31INo 2SX-263
32INo 2SX-578
33JNo 9SX-578
34KNo 2SX-123
35KNo 2SX-263
36KNo 2SX-578
Sheet2


Any ideas how I can modify the code?
Thank you.
BoyBoy
 
Upvote 0
How about
VBA Code:
Sub BoyBoy()
   Dim Ary As Variant
   Dim r As Long
   Dim Dic As Object
   Dim Cl As Range
   
   Ary = Sheets("Sheet2").Range("A1").CurrentRegion.Value2
   Set Dic = CreateObject("scripting.dictionary")
   Dic.CompareMode = 1
   For r = 2 To UBound(Ary)
      If Not Dic.Exists(Ary(r, 1)) Then Dic.Add Ary(r, 1), CreateObject("scripting.dictionary")
      Dic(Ary(r, 1))(Ary(r, 2)) = Dic(Ary(r, 1))(Ary(r, 2)) & Ary(r, 3) & "|"
   Next r
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Cl.Offset(, 5).Value = Dic(Cl.Value)(Cl.Offset(, 4).Value)
      Next Cl
      .Range("F2", .Range("F" & Rows.Count).End(xlUp)).TextToColumns .Range("F2"), xlDelimited, , , 0, 0, 0, 0, 1, "|"
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub BoyBoy()
   Dim Ary As Variant
   Dim r As Long
   Dim Dic As Object
   Dim Cl As Range
  
   Ary = Sheets("Sheet2").Range("A1").CurrentRegion.Value2
   Set Dic = CreateObject("scripting.dictionary")
   Dic.CompareMode = 1
   For r = 2 To UBound(Ary)
      If Not Dic.Exists(Ary(r, 1)) Then Dic.Add Ary(r, 1), CreateObject("scripting.dictionary")
      Dic(Ary(r, 1))(Ary(r, 2)) = Dic(Ary(r, 1))(Ary(r, 2)) & Ary(r, 3) & "|"
   Next r
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Cl.Offset(, 5).Value = Dic(Cl.Value)(Cl.Offset(, 4).Value)
      Next Cl
      .Range("F2", .Range("F" & Rows.Count).End(xlUp)).TextToColumns .Range("F2"), xlDelimited, , , 0, 0, 0, 0, 1, "|"
   End With
End Sub
This works great! The result is just the way I wanted, thank you very much for your help!
Have a wonderful day. :)
BoyBoy
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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