VB count unique values based on match

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
723
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

First, I'm running on MS Office Pro Plus 2016.

I have two sheets, (sheet1) is a list of email addresses in column A.
The second sheet (sheet2) has in column A a list of values, and in column B is the corresponding email address associated with the value in column A.

My goal here is to create a VB script that will match the email address in sheet1 column A with the email addresses in sheet2 column B, then give me the output of total unique values based on each matching email addresses in column B sheet1. I sure hope that makes sense. Thank you



Book1
AB
11email1@email.com
21email3@email.com
31email5@email.com
42email1@email.com
52email3@email.com
62email1@email.com
72email3@email.com
82email1@email.com
92email3@email.com
103email2@email.com
113email3@email.com
123email4@email.com
133email5@email.com
1410email2@email.com
1510email3@email.com
1610email5@email.com
179email1@email.com
189email2@email.com
199email3@email.com
209email5@email.com
214email1@email.com
224email3@email.com
234email5@email.com
245email1@email.com
255email2@email.com
265email3@email.com
275email4@email.com
285email5@email.com
296email1@email.com
306email3@email.com
316email5@email.com
327email3@email.com
337email5@email.com
348email3@email.com
358email5@email.com
369email1@email.com
379email2@email.com
389email3@email.com
399email5@email.com
4011email1@email.com
4111email2@email.com
4211email3@email.com
4311email4@email.com
4411email5@email.com
4512email1@email.com
4612email2@email.com
4712email3@email.com
4812email4@email.com
4912email5@email.com
5010email1@email.com
5110email2@email.com
5210email3@email.com
5310email5@email.com
5410email1@email.com
5510email2@email.com
5610email3@email.com
5710email5@email.com
5810email1@email.com
5910email2@email.com
6010email3@email.com
6110email5@email.com
6210email1@email.com
6310email2@email.com
6410email3@email.com
6510email5@email.com
6613email1@email.com
6713email3@email.com
6813email5@email.com
6914email3@email.com
7014email5@email.com
7114email1@email.com
7214email3@email.com
7314email5@email.com
7414email1@email.com
7514email3@email.com
7614email5@email.com
7714email1@email.com
7814email3@email.com
7914email5@email.com
8015email3@email.com
8116email3@email.com
8217email3@email.com
Sheet2
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Technically, I really only need to figure out what the formula would be. I can always use the formula in my script. Thanks again.
 
Upvote 0
Make sure you have headers on both sheets

VBA Code:
Sub jec()
  ar = Sheets(1).Cells(1, 1).CurrentRegion
  ar2 = Sheets(2).Cells(1, 1).CurrentRegion
 
  With CreateObject("scripting.dictionary")
    For i = 2 To UBound(ar)
      For j = 2 To UBound(ar2)
         If ar(i, 1) = ar2(j, 2) Then
            If Not .exists(ar(i, 1)) Then
              .Item(ar(i, 1)) = Array(ar(i, 1), 1)
            Else
               a = .Item(ar(i, 1))
               a(1) = a(1) + 1
              .Item(ar(i, 1)) = a
            End If
         End If
       Next
     Next
    Sheets(1).Cells(2, 1).Resize(.Count, 2) = Application.Index(.items, 0, 0)
  End With
End Sub
 
Upvote 0
Here's a formula:

Book1 (version 1).xlsb
AB
1EmailUnique
2email1@email.com11
3email2@email.com6
4email3@email.com17
5email4@email.com4
6email5@email.com13
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=SUM(SIGN(FREQUENCY(IF(Sheet2!$B$1:$B$82=A2,MATCH(Sheet2!$A$1:$A$82,Sheet2!$A$1:$A$82,0)),ROW(Sheet2!$A$1:$A$82)-ROW(Sheet2!$A$1)+1)))
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
Sheet2!_FilterDatabase=Sheet2!$B$1:$B$82B2:B6
 
Upvote 0
Solution
Thank you very much to both *JEC for the script and Eric for the formula. In the end, I'm going to go with the formula only because I understand it and can manipulate it if need be. The script *JEC does the job, I'm just not at the level to understand what you did. One day I'm hoping to get there.

I used the formula in my script.

VBA Code:
Sub ABS_0_Count()
    Range("B2").FormulaArray = "=SUM(SIGN(FREQUENCY(IF(Sheet2!$B$1:$B$82=A2,MATCH(Sheet2!$A$1:$A$82,Sheet2!$A$1:$A$82,0)),ROW(Sheet2!$A$1:$A$82)-ROW(Sheet2!$A$1)+1)))"
    Selection.AutoFill Destination:=Range("B2:B6"), Type:=xlFillDefault
End Sub

Thanks again
 
Upvote 0
Noticed different outcomes, compared to Eric's formula.
This adaption should give the same results

VBA Code:
Sub jec2()
  ar = Sheets(1).Cells(1, 1).CurrentRegion
  ar2 = Sheets(2).Cells(1, 1).CurrentRegion
 
  With CreateObject("scripting.dictionary")
    For i = 2 To UBound(ar)
      For j = 2 To UBound(ar2)
         If ar(i, 1) = ar2(j, 2) Then
           If .Exists(ar(i, 1)) Then
              If InStr(Join(Application.Index(.Item(ar(i, 1)), 0, 3)), ar2(j, 1)) = 0 Then
                a = .Item(ar(i, 1))
                a(1) = a(1) + 1
                a(2) = a(2) & "|" & ar2(j, 1)
               .Item(ar(i, 1)) = a
              End If
            Else
              .Item(ar(i, 1)) = Array(ar(i, 1), 1, ar2(j, 1))
          End If
        End If
      Next
    Next
  Sheets(1).Cells(2, 1).Resize(.Count, 2) = Application.Index(.Items, 0, 0)
 End With
End Sub
 
Upvote 0
Maybe easier to understand


VBA Code:
Sub jec3()
  ar = Sheets(1).Cells(1, 1).CurrentRegion
  ar2 = Sheets(2).Cells(1, 1).CurrentRegion
  
  For i = 2 To UBound(ar)
     For j = 2 To UBound(ar2)
       If ar(i, 1) = ar2(j, 2) Then
          If InStr(c00, ar2(j, 1)) = 0 Then a = a + 1
          c00 = c00 & "|" & ar2(j, 1)
       End If
     Next
     ar(i, 2) = a
     c00 = Empty: a = Empty
  Next
  Sheets(1).Cells(1, 1).CurrentRegion = ar
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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