VBA Dictionary - unique Account no with Items

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team ,

Need dictionary help, I want unique account Number ,
By comparing current months column to Previous Columns and vice versa.

Also I want Dictionary Items of that account no.


Below is my attempted code, which help in extracting unique account by Comparing both the Columns.
in G AND in Column J.


VBA Code:
Sub Add_Unique_Value()
  
  Dim sht_C As Worksheet
  Set sht = ThisWorkbook.Worksheets(1)
  
   
   Dim ary_base As Variant
   Dim ary_Compare As Variant


   'Store two Columns into Array
        With sht
                ary_base = .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp)).Value2
        End With

        With sht
            ary_Compare = .Range(.Cells(2, 2), .Cells(Rows.Count, 2).End(xlUp)).Value2
        End With
    
   Dim dict_CurrentMonth As Dictionary
   Set dict_CurrentMonth = Compare2List(ary_Compare, ary_base)
   
   
  sht.Range("d2").Resize(dict_CurrentMonth.Count).Value = Application.Transpose(dict_CurrentMonth.Keys)


    Dim Dict_PreviousMonth As Dictionary
   Set Dict_PreviousMonth = Compare2List(ary_base, ary_Compare)
   
sht.Range("F2").Resize(Dict_PreviousMonth.Count).Value = Application.Transpose(Dict_PreviousMonth.Keys)


End Sub
   


Public Function Compare2List(ary_base As Variant, ary_Compare As Variant) As Dictionary
   
    Dim i As Long
    Dim Dict As New Scripting.Dictionary

     With Dict

            For i = LBound(ary_base) To UBound(ary_base)
                .Item(ary_base(i, 1)) = "Empty"
                 .Item(ary_base(i, 1)) = Array(ary_base(i, 1))

            Next i

            For i = LBound(ary_Compare) To UBound(ary_Compare)
                If .Exists(ary_Compare(i, 1)) Then .Remove (ary_Compare(i, 1))
            Next i

    End With

    Set Compare2List = Dict

End Function

Input Data,

Book1
ABCDE
1Previous Months Account NumberSales ManCurrent Months Account NumberSalesMan
270Dhoni27Dhoni
310sachin36sachin
420Sehwag39Sehwag
590Deepika19Deepika
630Katrina28Katrina
770Saif30Saif
880Sharukh31Sharukh
920Jadeja24Jadeja
1010Pranay18Pranay
1190Vinay22Vinay
1230Ponting38Ponting
1390Petersen80Petersen
1440Gilchrist16Gilchrist
1590Dravid21Dravid
1640Lara23Dhoni
1717sachin
1835Sehwag
1932Deepika
2032Katrina
2132Saif
2232Sharukh
2332Jadeja
2432Pranay
2532Vinay
2632Ponting
2732Petersen
Sheet1



Expected Output , Column g and J able to extract via vba,
Got salesman by manually vlookup. Thanks

Book1
GHIJK
1Current Months Record not Found in PreviousSalesManPrevious Months Record not Found in PreviousSalesMan
227Dhoni70Dhoni
336sachin10sachin
439Sehwag20Sehwag
519Deepika90Deepika
628Katrina40Gilchrist
731Sharukh
824Jadeja
918Pranay
1022Vinay
1138Ponting
1216Gilchrist
1321Dravid
1423Dhoni
1517sachin
1635Sehwag
1732Deepika
Sheet1
Cell Formulas
RangeFormula
H2:H17H2=VLOOKUP(G2,$D$2:$E$27,2,0)




Thanks
mg
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try the following.
Considering the input data in columns A, B, D and E.
Results include accounts and salesman

VBA Code:
Sub Unique_Account()
  Dim arrA As Variant, arrD As Variant
  Dim dic1 As Object, dic2 As Object
  Dim i As Long
  
  arrA = Range("A2", Range("B" & Rows.Count).End(3)).Value2
  arrD = Range("D2", Range("E" & Rows.Count).End(3)).Value2
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  
  For i = 1 To UBound(arrA)
    If Not dic1.exists(arrA(i, 1)) Then dic1(arrA(i, 1)) = arrA(i, 2)
  Next
  
  For i = 1 To UBound(arrD)
    If Not dic1.exists(arrD(i, 1)) Then
      If Not dic2.exists(arrD(i, 1)) Then dic2(arrD(i, 1)) = arrD(i, 2)
    Else
      dic1.Remove arrD(i, 1)
    End If
  Next
  
  Range("G2").Resize(dic2.Count, 2).Value = Application.Transpose(Array(dic2.keys, dic2.items))
  Range("J2").Resize(dic1.Count, 2).Value = Application.Transpose(Array(dic1.keys, dic1.items))
End Sub
 
Upvote 0
Hi Danteamor,

Thanks for such a nice code, will check and revert.

Thanks
mg
 
Upvote 0
Hi DanteAmor,

Perfect ! your code is giving correct output (y) ?.

1) if there is a blank in any cell dictionary should not add blank as a key.
2) Can we add multiple columns into Dictionary Items like Array(arrD(i,2)),arrd(i,3))
and how to print the same.

I have added extra Amount Column, also there is one blank cell ,
blank should not be key.
how to add amount column to dictionary Items in the same code and print the output.



Below is Input Columns with Expected output.

Book1
ABCDEFGHIJK
1Previous Months Account NumberSales ManCurrent Months Account NumberSalesManamountDictionaryKEYDictionaryItem1DictionaryItem -2
270Dhoni27Dhoni42012770Dhoni
310sachin36sachin27143610sachin
420Sehwag39Sehwag46233920Sehwag
590Deepika19Deepika25431990Deepika
630Katrina28Katrina22372840Gilchrist
770Saif31Sharukh2298
880Sharukh30Saif137724Jadeja2381
920Jadeja31Sharukh229818Pranay2105
1010Pranay24Jadeja238122Vinay1142
1190Vinay18Pranay210538Ponting3534
1230Ponting22Vinay114216Gilchrist4714
1390Petersen38Ponting353421Dravid2149
1440Gilchrist80Petersen469323Dhoni2682
1590Dravid16Gilchrist471417sachin2279
1640Lara21Dravid214935Sehwag1578
1723Dhoni268232Deepika2027
1817sachin2279
1935Sehwag1578
2032Deepika2027
2132Katrina4484
2232Saif2759
2332Sharukh2895
2432Jadeja4437
2532Pranay3700
2632Vinay4881
2732Ponting4574
2832Petersen1074
Sheet1
Cell Formulas
RangeFormula
K7:K17K7=VLOOKUP(I7,D9:F34,3,0)



Thanks
mg
 
Upvote 0
Below is Input Columns with Expected output.
I don't understand the output.
Why in the output you have 27-70-Dhoni and you don't have 31-80-2298 or 32-90-2027?
It is very confusing for me.
It looks like a new macro and maybe a new thread you should create.
 
Upvote 0
Hi Danteamor,

Why in the output you have 27-70-Dhoni and you don't have 31-80-2298 or 32-90-2027?

Regarding above question, you have used dictionary and it will not add duplicate keys, it replace existing key with new key.
in my question there were duplicate Keys entries... because of this there is a confusion, But in Actual data there will not be duplicates.

and later I brought the anser using vlookup, which gives First Matching values.

Your approach is right, I checked it gives correct ouput. ignore my expected ouput,
Just i want to to add another columns into Items.... like Array below and ignore blank cell value as a key.

If Not dic2.exists(arrD(i, 1)) Then dic2(arrD(i, 1)) = Array(arrD(i, 2),arrD(i, 3))


hope it will clear , else I will post fresh thread with actual requirement.

Thanks
mg
 
Upvote 0
ignore blank cell value as a key
Try this:
VBA Code:
Sub Unique_Account()
  Dim arrA As Variant, arrD As Variant
  Dim dic1 As Object, dic2 As Object
  Dim i As Long
  
  arrA = Range("A2", Range("B" & Rows.Count).End(3)).Value2
  arrD = Range("D2", Range("E" & Rows.Count).End(3)).Value2
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  
  For i = 1 To UBound(arrA)
    if arrA(i,1) <> "" then If Not dic1.exists(arrA(i, 1)) Then dic1(arrA(i, 1)) = arrA(i, 2)
  Next
  
  For i = 1 To UBound(arrD)
    if arrD(i,1) <> "" then
    If Not dic1.exists(arrD(i, 1)) Then
      If Not dic2.exists(arrD(i, 1)) Then dic2(arrD(i, 1)) = arrD(i, 2)
    Else
      dic1.Remove arrD(i, 1)
    End If
    End If
  Next
  
  Range("G2").Resize(dic2.Count, 2).Value = Application.Transpose(Array(dic2.keys, dic2.items))
  Range("J2").Resize(dic1.Count, 2).Value = Application.Transpose(Array(dic1.keys, dic1.items))
End Sub

---------------------------------------------------------------------------------------------------
Just i want to to add another columns into Items
I still don't understand that part and without a real example of your data and the expected result, I can't help you.
 
Upvote 0
Hi Danteamor,

Thanks once again for your help, its working, ?

reg extra column help, I will correct my input and expected output and post a new thread.


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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