vba Find matching value from one sheet to another

bluefeather8989

Active Member
Joined
Nov 20, 2009
Messages
323
Office Version
  1. 365
Platform
  1. Windows
I have 2 worksheets in the same workbook (List, Data). In the data sheet I have a small list of items with the a number next to it.. I need a marco to input the numbers from data sheet to the corresponding items in List sheet.
Example: Data sheet 6 tomatos will input the number 6 next to tomatos in List sheet

Data
Shopping list V.2020.xlsx
PQ
26Tomatoes
31Bread white **
41Hamburger buns
51Hawaii buns
61Hotdog buns
71Bagels
86Coffee creamer
91Beef broth
103Ragu
112Spaghetti 1lb pkg
121Spaghetti (Twist)
131Pecans 2lb bag
141Mountain Dew
1512Bai drink
164Potato chips
172Ham Steaks
182Polish sausage
191Loin Country Style Ribs (Pork)
201Cubed beef
211Roast
221Corn on cob
Data
.
List
Shopping list V.2020.xlsx
CD
3Aspirin for heart
4Aunt Jemima syrup
5Axe 2 in 1 shampoo
6Bacon
7Bagels
8Bai drink
9Beef broth
10Black trash bags
11Bounce dryer sheets
12Bread wheat
13Bread white **
14Brown gravy mix
15Brown rice
16Butter
17Butter (Spray) **
18Butterscotch (Werthers original)
19Carpet fresh powder
20Carrots
21Cascade Platinum
22Cascade power dry (Dish Washer)
23Cat food
24Cat treats
25Celery
26Charmin ultra soft TP
27Cheese Velveeta slices
28Cheese White
29Cheese yellow
30Chicken Breast (not frozen)
31Chicken broth
32Chicken legs (not frozen)
33Chicken Thighs (not frozen)
34Chili
35Club *******s
36Coffee creamer
37Coffee Folgers classic roast
38Cooking Spray butter
39Cool whip
40Corn
41Corn chips
42Corn on cob
43Cream cheese Philadelphia
44Cream of celery
45Cream of chicken
46Creamed corn
47Crisco cooking oil
48Cubed beef
49Dawn dish soap large
50Dental floss picks
51Diced tomatoes
52Downey softener
53Eggs
54Febreze
55Flower 5lb bag
56French fried onions
57French fries
58Frozen Broccoli
59Frozen green beans
60Frozen Lima beans
61Garlic croutons
62Gorton breaded fish 26pkg.
63Green/yellow sponges
64Ham Steaks
65Hamburger 12 pkg
66Hamburger 1lb pkg
67Hamburger buns
68Hawaii buns
69Hines 57
70Hotdog buns
71Hotdogs
72Hydrogen peroxide
73Italian green beans
74Jell-O
75Jimmy Dean sausage
76ketchup 2pkg
77Kidney beans ( dark red)
78Kitty litter
79Kleenex 3 pkg
80Kraft Mac n cheese 5pkg.
81Lasagna
82Laundry soap Gain lavender
83Lemon juice
84Lipton tea bags decaf
85Liquid hand soap
86Loin Country Style Ribs (Pork)
87Magic eraser wipes
88Mandarin oranges
89Milk
90Mountain Dew
91Mt. Olive dill pickles (long)
92Mustard
93Napkins (Vanity fair)
94Neosporin with pain relief
95Noodles
96NyQuil green
97Oatmeal Quaker Oats 1 min.
98Olay soap
99Olive oil extra virgin
100Onion
101Onion powder
102Pace chunky salsa
103Pancake mix
104Pantene conditioner
105Paper plates
106Parmigiana cheese
107Peanut butter
108Pecans 2lb bag
109Pepper McCormick's
110Pepsi
111Pine sol
112Polish sausage
113Popcorn kettle corn
114Potato chips
115Potato sticks
116Potatoes
117Puffs 3pkg
118Quaker oatmeal 1minute
119Ragu
120Relish
121Reynolds aluminum foil
122Roast
123Salad dressing (Italian)
124Salt (Iodized)
125Sensodyne
126Shredded lettuce
127Shredded mini wheats
128Silver floss shredded Sauerkraut
129Slab Pork Ribs
130Soup (Chicken noodle)
131Soup (Tomato)
132Spaghetti (Twist)
133Spaghetti 1lb pkg
134Sugar 5lb bag
135Sunflower seeds
136Sweet n low
137Tacos shells (Hard)
138Tacos shells (soft)
139Tartar sauce
140Tater tots
141Tide liquid
142Tomato sauce
143Tomatoes
144Tooth picks
145Toothpaste Sensodyne
146Toothpaste Ultra bright
147Velveeta shells n Cheese 3pkg.
148Vinegar gal
149Weber's Chicago steak seasoning
150Welches grape jelly
151Worcestershire sauce
152Zip lock bags 1 qtr.
153Zip lock bags 1gal
List
Cell Formulas
RangeFormula
D3:D153D3=IF(Data!D3="","ZZ",Data!D3)
 
Last edited:

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).
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim Arr As Variant, i As Long, srcRng As Range, x As Long, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Data")
    Set desWS = Sheets("List")
    lRow = desWS.Range("D" & Rows.Count).End(xlUp).Row
    lRow2 = srcWS.Range("P" & Rows.Count).End(xlUp).Row
    Set srcRng = srcWS.Range("Q2:Q" & lRow2)
    Arr = desWS.Range("D2:D" & lRow).Value
    For i = LBound(Arr) To UBound(Arr)
        If Not IsError(Application.Match(Arr(i, 1), srcRng, 0)) Then
            x = Application.Match(Arr(i, 1), srcRng, 0)
            desWS.Range("C" & i + 1) = srcWS.Range("P" & x + 1)
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
As it can be obviously achieved directly with a beginner level formula but if a VBA procedure is really necessary so just using the same formula :​
VBA Code:
Sub Demo1()
    With Sheets("List").Range("C3", [List!D3].End(xlDown)(1, 0))
        .Formula = Replace("=IFERROR(INDEX(Data!$P$2:$P$#,MATCH(D3,Data!$Q$2:$Q$#,0)),"""")", "#", [Data!Q2].End(xlDown).Row)
        .Formula = .Value2
    End With
End Sub
 
Upvote 0
mumps,​
to avoid a duplicate MATCH calculation it should be obvious to place the codeline x = before the codeline If Not IsError
just with the variable x declared as Variant …​
 
Upvote 0
@Marc L
Thank you for that. I just thought that given the type of posted data, there would be no duplicate matches. :)
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,955
Latest member
BatCoder

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