Comparing Data from 2 Sheets and Copy using VBA

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello all, I have data in sheet1 and sheet2. Sheet1 is source data and sheet2 is input. I want to compare data in column A and B of both sheets. i.e comparing as combinations. In the example below, the VBA has to look for combinations, if it matches in both sheets then data in sheet2 B column has to be copied to sheet1 C column. If no matching combination available then it should be leave blank. Please refer the example for more clarity. THank you

Input Sheet1

Book6
AB
1Reg. NumberNote
22019JECAE222English Book
32019JECAE222Maths Book
42019JECAE222Physics Book
52019JECAE222Chemistry Book
62019JECAE223English Book
72019JECAE223Maths Book
82019JECAE223Chemistry Book
92019JECAE224Maths Book
102019JECAE224Physics Book
112019JECAE224Chemistry Book
Sheet1


Input Sheet2

Book6
AB
1Reg. NumberNote
22019JECAE222English Book
32019JECAE222Chemistry Book
42019JECAE223English Book
52019JECAE223Chemistry Book
62019JECAE224Maths Book
72019JECAE224Chemistry Book
Sheet2


Output in Sheet1

Book6
ABC
1Reg. NumberNotefrom sheet2 column B
22019JECAE222English BookEnglish Book
32019JECAE222Maths Book
42019JECAE222Physics Book
52019JECAE222Chemistry BookChemistry Book
62019JECAE223English BookEnglish Book
72019JECAE223Maths Book
82019JECAE223Chemistry BookChemistry Book
92019JECAE224Maths BookMaths Book
102019JECAE224Physics Book
112019JECAE224Chemistry BookChemistry Book
Sheet1
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
In C2 of sheet1: (close that one with ctrl+shift+return and fill down, its a matrix formula)

Excel Formula:
=IFERROR(INDEX(Sheet2!$B$2:$B$7,MATCH(A2&B2,Sheet2!$A$2:$A$7&Sheet2!$B$2:$B$7,0)),"")
 
Upvote 0
In C2 of sheet1: (close that one with ctrl+shift+return and fill down, its a matrix formula)

Excel Formula:
=IFERROR(INDEX(Sheet2!$B$2:$B$7,MATCH(A2&B2,Sheet2!$A$2:$A$7&Sheet2!$B$2:$B$7,0)),"")
Hey JEC, it is working as expected but my requirement is with VBA solution. Thanks for your time.
 
Upvote 0
Hi, @madhuchelliah
Try this:
VBA Code:
Sub a1180373a()
Dim i As Long
Dim tx As String
Dim va, vb, vc
Dim d As Object

With Sheets("Sheet2")
    va = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

Set d = CreateObject("scripting.dictionary")
d.CompareMode = vbTextCompare

For i = 1 To UBound(va, 1)
    d(va(i, 1) & "|" & va(i, 2)) = va(i, 2)
Next


With Sheets("Sheet1")
    vb = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row)

    ReDim vc(1 To UBound(vb, 1), 1 To 1)
    
    For i = 1 To UBound(vb, 1)
        tx = vb(i, 1) & "|" & vb(i, 2)
        If d.Exists(tx) Then vc(i, 1) = d(tx)
    Next
    
    .Range("C2").Resize(UBound(vc, 1), 1) = vc

End With
End Sub
 
Upvote 0
Solution
Hi, @madhuchelliah
Try this:
VBA Code:
Sub a1180373a()
Dim i As Long
Dim tx As String
Dim va, vb, vc
Dim d As Object

With Sheets("Sheet2")
    va = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

Set d = CreateObject("scripting.dictionary")
d.CompareMode = vbTextCompare

For i = 1 To UBound(va, 1)
    d(va(i, 1) & "|" & va(i, 2)) = va(i, 2)
Next


With Sheets("Sheet1")
    vb = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row)

    ReDim vc(1 To UBound(vb, 1), 1 To 1)
   
    For i = 1 To UBound(vb, 1)
        tx = vb(i, 1) & "|" & vb(i, 2)
        If d.Exists(tx) Then vc(i, 1) = d(tx)
    Next
   
    .Range("C2").Resize(UBound(vc, 1), 1) = vc

End With
End Sub
Hey Akuini, it is working as expected. Thanks for your time.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
One more

VBA Code:
Sub jvr()
  ar = Sheets(1).Cells(1).CurrentRegion.Resize(, 3)
  ar2 = Sheets(2).Cells(1).CurrentRegion.Resize(, 3)
  
  For i = 2 To UBound(ar2)
    ar2(i, 3) = ar2(i, 1) & ar2(i, 2)
  Next
  
  For i = 2 To UBound(ar)
    x = Application.Match(ar(i, 1) & ar(i, 2), Application.Index(ar2, 0, 3), 0)
    If IsNumeric(x) Then ar(i, 3) = Application.Index(ar2, x, 2)
  Next
    
  Sheets(1).Cells(1).CurrentRegion.Resize(, 3) = ar
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,835
Members
449,343
Latest member
DEWS2031

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