Need to compare two columns and fetch the matching values in a new column

aghaffar82

Board Regular
Joined
Jun 13, 2019
Messages
65
Office Version
  1. 365
Platform
  1. Windows
Kindly check the attached and advise how can this be achieved. Thank you in advance.

data for question.xlsx
ABCDEFGHIJ
1Value1Value2InvPriceValue1Value2InvPrice
2ABCGHI527.94ABCABC527.94
3GHINone455.94GHIGHI455.94
4DMOPPM455.94PQSPQS284.94
5PQSAGI284.94AGIAGI584.94
6TTPDGS184.94
7AGIPQS584.94
8TOFABC684.94
9
10
11ORIGINAL DATADESIRED RESULT
12.11.20.dups (1)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Does this do what you want?

20 12 12.xlsm
ABCDEFGHIJ
1Value1Value2InvPriceValue1Value2InvPrice
2ABCGHI527.94ABCABC527.94
3GHINone455.94GHIGHI455.94
4DMOPPM455.94PQSPQS284.94
5PQSAGI284.94AGIAGI584.94
6TTPDGS184.94
7AGIPQS584.94
8TOFABC684.94
Match columns
Cell Formulas
RangeFormula
G2:J5G2=FILTER(INDEX(A2:D8,SEQUENCE(ROWS(A2:D8)),{1,1,3,4}),ISNUMBER(MATCH(A2:A8,B2:B8,0)))
Dynamic array formulas.
 
Upvote 0
Solution
Hi aghaffar82,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim cln As New Collection
    Dim lngLastRow As Long, lngMyRow As Long, lngPasteRow As Long
    Dim varMyCol As Variant
    Dim strMyValues() As String
    
    Application.ScreenUpdating = False
    
    lngLastRow = Range("A:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lngPasteRow = Range("G:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    
    For Each varMyCol In Array("A", "B")
        For lngMyRow = 2 To lngLastRow
            On Error Resume Next
                cln.Add Range("C" & lngMyRow) & "|" & Range("D" & lngMyRow), CStr(Range(varMyCol & lngMyRow))
                If Err.Number <> 0 Then
                    Range("G" & lngPasteRow & ":H" & lngPasteRow).Value = CStr(Range(varMyCol & lngMyRow))
                    strMyValues = Split(cln.Item(Range(varMyCol & lngMyRow)), "|")
                    Range("I" & lngPasteRow).Value = CDbl(strMyValues(0))
                    Range("J" & lngPasteRow).Value = CDbl(strMyValues(1))
                    lngPasteRow = lngPasteRow + 1
                End If
            On Error GoTo 0
        Next lngMyRow
    Next varMyCol
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Thank you so much Sir, I will test both of the solutions and update which one runs quicker, I am certain both the solutions are perfect but as the data would be very long, (thousands of rows) I would prefer the one which runs faster and Excel doesn't crash. Thank you again.
 
Upvote 0
Does this do what you want?

20 12 12.xlsm
ABCDEFGHIJ
1Value1Value2InvPriceValue1Value2InvPrice
2ABCGHI527.94ABCABC527.94
3GHINone455.94GHIGHI455.94
4DMOPPM455.94PQSPQS284.94
5PQSAGI284.94AGIAGI584.94
6TTPDGS184.94
7AGIPQS584.94
8TOFABC684.94
Match columns
Cell Formulas
RangeFormula
G2:J5G2=FILTER(INDEX(A2:D8,SEQUENCE(ROWS(A2:D8)),{1,1,3,4}),ISNUMBER(MATCH(A2:A8,B2:B8,0)))
Dynamic array formulas.
Sir, could you please tweak the formula a bit, I want to match the value of H2 and then pull the corresponding value from column C & D into I & J. So, technically, the lookup value remains the same but as the values in column C & D would differ based on the lookup table row. So, if I were using VLOOKUP, I would use it like this in I2:
=VLOOKUP(H2,B:D,2,FLASE) and (for price column J) in J2 =VLOOKUP(H2,B:D,3,FLASE).

Hopefully, that makes sense.
 
Upvote 0
Thank you, I wanted to stay dynamic and was able to create another array formula for I & J
=FILTER(C:D,ISNUMBER(MATCH(B:B,H:H,0)),"No data")

The issue with VLOOKUP was it wouldn't update automatically for the new rows.

Best Regards
 
Upvote 0
Thank you, I wanted to stay dynamic and was able to create another array formula for I & J
=FILTER(C:D,ISNUMBER(MATCH(B:B,H:H,0)),"No data")
That does not produce the results you said you wanted? :confused:

20 12 12.xlsm
ABCDEFGHIJKLMNO
1Value1Value2InvPriceValue1Value2InvPriceValue1Value2InvPrice
2ABCGHI527.94ABC527.94ABCABC527.94
3GHINone455.94GHI284.94GHIGHI455.94
4DMOPPM455.94PQS584.94PQSPQS284.94
5PQSAGI284.94AGI684.94AGIAGI584.94
6TTPDGS184.94
7AGIPQS584.94
8TOFABC684.94DESIRED RESULT
Match columns
Cell Formulas
RangeFormula
I1:J5I1=FILTER(C:D,ISNUMBER(MATCH(B:B,H:H,0)),"No data")
Dynamic array formulas.


I would also highly recommend not to use whole column references in a formula like that. On my machine that formula takes about 70 times as long to calculate as say
Excel Formula:
=FILTER(C1:D1000,ISNUMBER(MATCH(B1:B1000,H1:H1000,0)),"No data")
 
Upvote 0
Yes, I double-checked and you are absolutely right it is not producing the result I want.

My aim is to match the value of the item column (column B and then pull the inv from column C & Price from column D into the columns N & O). And I don't want to use the VLOOKUP. If I match the values in column A, the corresponding rows have different numbers in Inv & Price columns as those rows would have different item numbers.

Secondly, although the formula doesn't produce the results as desired, it doesn't take more than a few seconds to populate. The reason I have to use the whole column is that the number of rows to be inserted in columns A to D are unknown often and can go way beyond 100k sometimes.

Thank you so much for looking at it again and pointing out the wrong results. Regards
 
Upvote 0
Matching column A rather than column B seems to produce the results you gave before?

20 12 12.xlsm
ABCDEFGHIJ
1Value1Value2InvPriceValue1Value2InvPrice
2ABCGHI527.94ABC527.94
3GHINone455.94GHI455.94
4DMOPPM455.94PQS284.94
5PQSAGI284.94AGI584.94
6TTPDGS184.94
7AGIPQS584.94
8TOFABC684.94
Match columns
Cell Formulas
RangeFormula
I2:J5I2=FILTER(C:D,ISNUMBER(MATCH(A:A,H:H,0)),"No data")
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

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