Compare 2 columns and find duplicates then copy corresponding cell into another cell

gray_b

New Member
Joined
Apr 17, 2019
Messages
34
Office Version
  1. 365
Platform
  1. Windows
On the same workpage I have column 'H' of a 1000+ text cells, and another column 'B' of 2000+ text cells.

There are many duplicate text cells in column 'B'. But the corresponding text cells in column 'A' always stays the same in relationship to text cells in column 'B'

I need to go through each cell in turn from column 'H' and find a duplicate text cell in column 'B'.

When a duplicate cell is found, then copy its corresponding cell text from cells in column 'A' and paste into column 'G'.

Then onto the next cell in column 'H'

Screenshot 2023-12-13 212634.png


Screenshot 2023-12-13 212706.png
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Could you please provide a copy of your data using the XL2BB add in so helpers don't have to type out a load of data to test code/formulas - or alternatively share your file via Google Drive, Dropbox or similar file sharing platform? It would also help if you could update your profile to show what version of Excel you're using, as solutions may vary depending on which version you have. Thank you in anticipation.
 
Upvote 0
Hi - Excel 365

Have a look at file here

It is a complete xlsm file, data included is not live data, only demo.

Excuse me for the coding, as i like to keep things simple and understandable in my older years.
 
Upvote 0
Thank you for that. I'm wondering whether it might be simpler to go for a formula solution rather than VBA? Here's just a suggestion, have a look at the XLOOKUP function in column G in the sample below & see if it is doing what you want. Please let me know if it isn't (I'll look at it tomorrow my time).
extract all products sold and quatify backup new 1.xlsm
ABCDEFGH
1Product ReferenceProduct NameProduct QuantityOrder IDProduct Name
210 x Grafting Package12344010 x Grafting Package
310 x Grafting Package123580100 x Grafting Package
410 x Grafting Package12398050 x Grafting Package
510 x Grafting Package12401Malus 1705-000Apple - Acme - Scion
610 x Grafting Package12406Malus 8684Apple - Adams Pearmain - Barerooted
710 x Grafting Package12420Malus 833-J18Apple - Adams Pearmain - Scion
810 x Grafting Package12427Malus 3550-I53Apple - All Doer - Scion
9100 x Grafting Package12387Malus 5026-HL29Apple - Amanda - Scion
10100 x Grafting Package12451Malus 1613-J15Apple - American Mother - Scion
1150 x Grafting Package12323Malus 3750-HL11Apple - Annie Elizabeth - Scion
12Malus 1705-000Apple - Acme - Scion22357Malus 4928-F69Apple - Arap Kizi 'Arabian Girl' - Scion
13Malus 1705-000Apple - Acme - Scion12399Malus 2962-U10Apple - Aroma - Scion
14Malus 8684Apple - Adams Pearmain - Barerooted12431Malus 800-D09Apple - Arthur Turner - Scion
15Malus 833-J18Apple - Adams Pearmain - Scion12329Malus 8773Apple - Ashmeads Kernel - Barerooted
16Malus 833-J18Apple - Adams Pearmain - Scion12334Malus 3616-U29Apple - Ashmeads Kernel - Scion
17Malus 833-J18Apple - Adams Pearmain - Scion12354Malus 5641-X30Apple - Autumn Arctic - Scion
18Malus 833-J18Apple - Adams Pearmain - Scion12356Malus 1072-L11Apple - Bakers Delicious - Scion
19Malus 833-J18Apple - Adams Pearmain - Scion12384Malus 4900-W7Apple - Bakran - Scion
20Malus 833-J18Apple - Adams Pearmain - Scion12416Malus 2818-Q09Apple - Bardsey - Scion
21Malus 833-J18Apple - Adams Pearmain - Scion12417Malus 3749-HL07Apple - Barnack Orange - Scion
22Malus 833-J18Apple - Adams Pearmain - Scion12423Malus 5328-W25Apple - Batul - Scion
23Malus 833-J18Apple - Adams Pearmain - Scion22428Malus 1704-O17Apple - Beauty of Bath - Scion
24Malus 3550-I53Apple - All Doer - Scion12399Malus 1746-P08Apple - Bec d Oie - Scion
25Malus 3550-I53Apple - All Doer - Scion12430Malus 1718-P04Apple - Benoni - Scion
26Malus 5026-HL29Apple - Amanda - Scion12356Malus 1021-Q38Apple - Ben's Red - Scion
27Malus 5026-HL29Apple - Amanda - Scion12467Malus 1707-O21Apple - Blood Royal - Scion
28Malus 1613-J15Apple - American Mother - Scion12462Malus 2285-BW32Apple - Bloody Ploughman - Scion
29Malus 3750-HL11Apple - Annie Elizabeth - Scion12363Malus 1235-J39Apple - Braeburn - Scion
30Malus 4928-F69Apple - Arap Kizi 'Arabian Girl' - Scion12350Malus 792-D1Apple - Bramley - Scion
31Malus 4928-F69Apple - Arap Kizi 'Arabian Girl' - Scion12399Malus 1086-J28Apple - Bramley Clone 20 - Scion
export_orders_2023.02.26_8-59-1
Cell Formulas
RangeFormula
G2:G31G2=XLOOKUP(H2,$B$2:$B$1011,$A$2:$A$1011,"Not found",0,1)
 
Upvote 0
It certainly does the job.

I must admit that I have never used XLOOKUP but always VBA

I might be showing my incompetence, but can that formula be incorporated in VBA. As I do not know what the cell count is, as it can change.

and i use LRC = Cells(Rows.Count, "C").End(xlUp).Row
 
Upvote 0
It can be added via VBA, I'm away from my laptop now but I will get back to you tomorrow, unless another volunteer steps in beforehand.
 
Upvote 0
Here's some code to insert the Xlookup formula & values. I didn't hard code the sheet name as I suspect it might change - therefore, you must run the code with the export_orders sheet active.

VBA Code:
Option Explicit
Sub Get_XLOOKUP()
    Dim LRowB As Long, LRowH As Long
    LRowB = Cells(Rows.Count, "B").End(xlUp).Row
    LRowH = Cells(Rows.Count, "H").End(xlUp).Row

    With Range("G2:G" & LRowH)
        .Formula = "=XLOOKUP(H2,$B$2:$B$" & LRowB & ",$A$2:$A$" & LRowB & ",""Not found"",0,1)"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Solution
Excellent - I think I was no where near close with the code. And I did not put any " around the XLOOKUP statement.

Copy and pasted your code, and worked first time.

Many thanks, as I would never have sussed it out.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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