VBA: Comparing two tables and adding unique values from 1 table to another

Coyotex3

Well-known Member
Joined
Dec 12, 2021
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Looking for a simplified way of comparing two tables and adding the unique values from one table to another table.

I have these two tables:

Book1
ABCDE
1NameGradeNameGrade
2John12thJohn12th
3Jane15thJane15th
4Jack8thJack8th
5Mike8thJane15th
6Susy1stJack8th
Sheet2


Ideally I would like to get one table that shows this:

Book1
HI
1NameGrade
2John12th
3Jane15th
4Jack8th
5Jane15th
6Jack8th
7Mike8th
8Susy1st
Sheet2


Open to doing this outside of an Excel Table, if that is easier? The results can be added to the second table, or in a new table/range altogether.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Open to doing this outside of an Excel Table
That being the case, this is one suggestion:
Book1
ABCDEFGHIJ
1NameGradeNameGradeNameGrade
2John12thJohn12thJohn12th
3Jane15thJane15thJane15th
4Jack8thJack8thJack8th
5Mike8thJane15thMike8th
6Susy1stJack8thSusy1st
7
Sheet1
Cell Formulas
RangeFormula
H1:I6H1=UNIQUE(VSTACK(Table1[#All],Table2[#All]))
Dynamic array formulas.
 
Upvote 0
That doesn't produce the stated expected outcome though.
Doh!

Perhaps this then, although not in the same stated order:
Book1
ABCDEFGHIJ
1NameGradeNameGradeNameGrade
2John12thJohn12thJohn12th
3Jane15thJane15thJane15th
4Jack8thJack8thJack8th
5Mike8thJane15thMike8th
6Susy1stJack8thSusy1st
7John12th
8Jane15th
9Jack8th
10
Sheet1
Cell Formulas
RangeFormula
H1:I1H1=Table1[#Headers]
H2:I9H2=VSTACK(UNIQUE(Table1),UNIQUE(Table2))
Dynamic array formulas.
 
Upvote 0
Kevin, thanks for the answer. Ideally I’d like VBA for this one. As Peter mentioned the formula does not replicate the results desired.

@Peter_SSs sure thing. This was just some made up data.

On The real data the second table has three columns which contain some items, subitems, and a name.

The first table contains items and subitems which were not attributed to any of the names on the second table.

I’d like to get a list of those items and subitems in order to add it to the end of the second table, and then add something like “No name associated.” On the name column.

Hope this makes sense, if not, happy to share an example with data that makes more sense 😄
 
Upvote 0
Perhaps this then, although not in the same stated order:
.. but still different anyway (unless the full result was not shown in post 1). Stated result shows 8 rows including heading row. Yours has 9 rows.

Hope this makes sense,
Not to me. I still don't understand the sample results in post 1. :confused:

In any case it seems the structure of the sample data in post 1 is not like the structure of your real data. Perhaps a new set of sample data and expected results with the actual structure & explain again in relation to that?
 
Upvote 0
Sorry! I will try again.

Here is Range 1:
Book1
ABCD
1CategoryItemRevenue
2PlumbingItem A100
3Electricity Item A500
4EngineeringItem A1000
5PlumbingItem B1500
6Electricity Item B200
7EngineeringItem B2000
8AccountingItem B2000
9PlumbingItem C400
10Electricity Item C1000
11
12
Sheet3


And here is range 2:

Book1
HIJK
1CategoryItemWorker
2PlumbingItem AJack
3Electricity Item AJane
4EngineeringItem AJack
5PlumbingItem BJane
6Electricity Item BJack
7EngineeringItem BJane
8PlumbingItem CJane
9PlumbingItem AJosh
10Electricity Item AMike
11EngineeringItem AMike
12PlumbingItem BMike
13Electricity Item BSusy
14EngineeringItem BJack
15PlumbingItem CJack
16
Sheet3


Notice the highlighted rows, how Accounting Item B and Electricity Item C are not showing up on Range 2? However, there is income associated with those items, but no worker reported being the one responsible for the income. Therefore, I'd like to add those two rows to the bottom of Range 2 to get something like this.

Book1
HIJ
1CategoryItemWorker
2PlumbingItem AJack
3Electricity Item AJane
4EngineeringItem AJack
5PlumbingItem BJane
6Electricity Item BJack
7EngineeringItem BJane
8PlumbingItem CJane
9PlumbingItem AJosh
10Electricity Item AMike
11EngineeringItem AMike
12PlumbingItem BMike
13Electricity Item BSusy
14EngineeringItem BJack
15PlumbingItem CJack
16AccountingItem BNo Name associated
17Electricity Item CNo Name associated
Sheet3


Hopes this makes a little more sense. This will go inside of a larger code that is helping gather financial data.

PS. I left these as Ranges. Happy to work with either Range or Table.
 
Upvote 0
Hopes this makes a little more sense.
It does. (y)

Try this with a copy of your data.

VBA Code:
Sub Test()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("H2", Range("J" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    d(a(i, 1) & "|" & a(i, 2)) = 1
  Next i
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  Application.ScreenUpdating = False
  For i = 1 To UBound(a)
    If Not d.exists(a(i, 1) & "|" & a(i, 2)) Then
      d(a(i, 1) & "|" & a(i, 2)) = 1
      With Range("H" & Rows.Count).End(xlUp).Offset(1).Resize(, 3)
        .Cells(1).Value = a(i, 1): .Cells(2).Value = a(i, 2): .Cells(3).Value = "No Name associated"
      End With
    End If
  Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
It does. (y)

Try this with a copy of your data.

VBA Code:
Sub Test()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("H2", Range("J" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    d(a(i, 1) & "|" & a(i, 2)) = 1
  Next i
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  Application.ScreenUpdating = False
  For i = 1 To UBound(a)
    If Not d.exists(a(i, 1) & "|" & a(i, 2)) Then
      d(a(i, 1) & "|" & a(i, 2)) = 1
      With Range("H" & Rows.Count).End(xlUp).Offset(1).Resize(, 3)
        .Cells(1).Value = a(i, 1): .Cells(2).Value = a(i, 2): .Cells(3).Value = "No Name associated"
      End With
    End If
  Next i
  Application.ScreenUpdating = True
End Sub
Thanks Peter. I’ll give this code a run once I’m back on the laptop in a few hours and let you know!
 
Upvote 0
It does. (y)

Try this with a copy of your data.

VBA Code:
Sub Test()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("H2", Range("J" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    d(a(i, 1) & "|" & a(i, 2)) = 1
  Next i
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  Application.ScreenUpdating = False
  For i = 1 To UBound(a)
    If Not d.exists(a(i, 1) & "|" & a(i, 2)) Then
      d(a(i, 1) & "|" & a(i, 2)) = 1
      With Range("H" & Rows.Count).End(xlUp).Offset(1).Resize(, 3)
        .Cells(1).Value = a(i, 1): .Cells(2).Value = a(i, 2): .Cells(3).Value = "No Name associated"
      End With
    End If
  Next i
  Application.ScreenUpdating = True
End Sub
Thank you Peter. This works as expected!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
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