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.
 
@Peter_SSs Happy to start a new thread since this is a different question.

But would it be possible to perhaps show the revenue of all the items in Range 1 and add them to Range 2 Column J as such:

Book1
HIJKL
1CategoryItemWorkerRevenue
2PlumbingItem AJack100
3Electricity Item AJane500
4EngineeringItem AJack1000
5PlumbingItem BJane1500
6Electricity Item BJack200
7EngineeringItem BJane2000
8PlumbingItem CJane400
9PlumbingItem AJosh100
10Electricity Item AMike500
11EngineeringItem AMike1000
12PlumbingItem BMike1500
13Electricity Item BSusy200
14EngineeringItem BJack2000
15PlumbingItem CJack400
16AccountingItem BNo Name associated2000
17Electricity Item CNo Name associated1000
18
Sheet3
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Are sure you want to do that ?
Range 1 are unique combinations for Category and Item. Range 2 has multiple occurences of those combinations split by Worker.
By assigning the "total value" for each Category and Item to the each worker you are effectively going to be double counting. The totals won't match.
This is going to be very misleading.
 
Upvote 0
But would it be possible to perhaps show the revenue of all the items in Range 1 and add them to Range 2 Column J as such:
Try this

VBA Code:
Sub Test_v2()
  Dim d1 As Object, d2 As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, lrH As Long
  Dim s As String
  
  Set d1 = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
  b = Range("A2", Range("C" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(b)
    d2(b(i, 1) & "|" & b(i, 2)) = b(i, 3)
  Next i
  lrH = Range("H" & Rows.Count).End(xlUp).Row
  k = lrH - 1
  a = Range("H2:K" & k + 1 + UBound(b)).Value
  For i = 1 To k
    s = a(i, 1) & "|" & a(i, 2)
    d1(s) = 1
    a(i, 4) = d2(s)
  Next i
  For i = 1 To UBound(b)
    s = b(i, 1) & "|" & b(i, 2)
    If Not d1.exists(s) Then
      k = k + 1
      a(k, 1) = b(i, 1): a(k, 2) = b(i, 2): a(k, 3) = "No Name associated": a(k, 4) = d2(s)
    End If
  Next i
  Range("H2:K2").Resize(k).Value = a
End Sub
 
Upvote 1
Are sure you want to do that ?
Range 1 are unique combinations for Category and Item. Range 2 has multiple occurences of those combinations split by Worker.
By assigning the "total value" for each Category and Item to the each worker you are effectively going to be double counting. The totals won't match.
This is going to be very misleading.
Alex that is true!

The reason why I’d like to get the income displayed on the right column, is because later I will be multiplying this amount by the percentage of hours attributed to each employee to allocate the income proportionally.

This little piece of code is a small piece of a bigger code I’m working on revamping which is pulling different data from different sheets. It’s been a huge undertaking and I’ve been experimenting with trial and error to figure out what works and what doesn’t 😅.
 
Upvote 0
Try this

VBA Code:
Sub Test_v2()
  Dim d1 As Object, d2 As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, lrH As Long
  Dim s As String
 
  Set d1 = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
  b = Range("A2", Range("C" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(b)
    d2(b(i, 1) & "|" & b(i, 2)) = b(i, 3)
  Next i
  lrH = Range("H" & Rows.Count).End(xlUp).Row
  k = lrH - 1
  a = Range("H2:K" & k + 1 + UBound(b)).Value
  For i = 1 To k
    s = a(i, 1) & "|" & a(i, 2)
    d1(s) = 1
    a(i, 4) = d2(s)
  Next i
  For i = 1 To UBound(b)
    s = b(i, 1) & "|" & b(i, 2)
    If Not d1.exists(s) Then
      k = k + 1
      a(k, 1) = b(i, 1): a(k, 2) = b(i, 2): a(k, 3) = "No Name associated": a(k, 4) = d2(s)
    End If
  Next i
  Range("H2:K2").Resize(k).Value = a
End Sub
@Peter_SSs this is phenomenal. Can't really say I understand much how this array is working, but I love the array approach since it is faster and the lines of code tend to be shorter. The bigger code I'm working on utilizes a lot of worksheet functions as I don't really know how to do calculations within an array.

With your approach, is it possible to do calculations such as sumifs? Or does that need to be accomplished with FormulaR1C1 & Worksheet Functions?
 
Upvote 0
With your approach, is it possible to do calculations such as sumifs? Or does that need to be accomplished with FormulaR1C1 & Worksheet Functions?
I'm sure it would be possible but it is hard to say what the best approach would be without knowing the exact requirement(s).
 
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