VBA Help - dictionary highlight new Entry

Mallesh23

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

I am comparing two list and bringing Unique values.
I am able to get output of unique keys, But how to get Dictionary Items of that unique key as well. Thanks

I am storing data into Array as my data are different workbooks.


Below attempted code, gives correct unique keys except Items, how to get it.

VBA Code:
Sub Dictionary_Find_New_Entry()
    
    ary_Compare = Range("f2:f4").Value2
    ary_base = Range("A2:b5").Value2
    
        
    Dim dict As New Scripting.Dictionary
    Dim i As Long
    
    
     With dict
            .CompareMode = TextCompare
            For i = LBound(ary_base) To UBound(ary_base)
                .Item(ary_base(i, 2)) = "Empty"
                   If .Item(ary_base(i, 2)) <> "" Then
                      If Not .Exists(ary_base(i, 2)) Then
                            .Add (ary_base(i, 2)), (ary_base(i, 1))
                        End If
                    End If
            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
    
    Range("J1").value =  dict.Keys(0)  ' Giving correct output :=> Goods damaged in Transit
    
    Range("I1").value dict.Items(0)   :=> should give [B]37133[/B]
    

End Sub

Below is a Table , expected output I & j Columns

Book2
ABCDEFGHIJ
1Invoice noCommentsCommentExpected OutputKey-Dictionary
214656Goods in WareshouseGoods in Wareshouse37133Goods damaged in Transit
327524Goods Returnedgoods Returned
437133Goods damaged in TransitGoods Sold
523427Goods Sold
Sheet1



Thanks
mg
 
Hi Fluff and Yongle,

Wow it worked! (y)

Millions of thanks both of you for help, It has saved my lot of further work. ?


Thanks
mg
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Glad we could help & thanks for the feedback.
 
Upvote 0
Hi Fluff,

There is a small change from client side, they want all unique comments with invoice. Even duplicate also.
Below is a Table , Compare column F to Column B. Collate all unique comments with invoice duplicate Comments also.

Expected output is in Column I and J. is it possible to get output by modifying existing code in dictionary.

Thanks in advance for help.

Book1
ABCDEFGHIJ
1Invoice noCommentsComment CriteriaExpected OutputKey-Dictionary
214656Goods in WareshouseGoods in Wareshouse37133Goods damaged in Transit
327524Goods Returnedgoods Returned78909Goods damaged in Transit
437133Goods damaged in TransitGoods Sold80979Goods damaged in Transit
523427Goods Sold9797Goods Stolen
678909Goods damaged in Transit
780979Goods damaged in Transit
89797Goods Stolen
Sheet1



My Attempted CODE

VBA Code:
Sub Dictionary_Find_New_Entry()
    
    ary_Compare = Range("f2:f4").Value2
    ary_base = Range("A2:b8").Value2
    
        
    Dim dict As New Scripting.Dictionary
    Dim i As Long
    
    
     With dict
            .CompareMode = TextCompare
            For i = LBound(ary_base) To UBound(ary_base)
                   If .Item(ary_base(i, 1)) <> "" Then
                      If Not .Exists(ary_base(i, 1)) Then   'same code above changed item and key.
                            .Add (ary_base(i, 1)), (ary_base(i, 2))
                        End If
                    End If
            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
    
    'Range("J2").value = dict.Keys(0)   ' Giving correct output :=> Goods damaged in Transit
    Range("J2").value = dict.Items(0)
    

End Sub


Thanks
mg
 
Upvote 0
Hi Fluff,

I tried with below code it is giving correct output.
But I am using Double dictionary here,

Can we get output using Dictionary. Just for learning purpose. Thanks.


VBA Code:
Sub Dictionary_Find_New_Entry()
    
    ary_compare = Range("f2:f4").Value2
    ary_base = Range("A2:b8").Value2
    
        
    Dim dict As New Scripting.Dictionary
    Dim Dict2 As New Scripting.Dictionary
    
    Dim i As Long
    
    'Store Column F Unique values
     With dict
          .CompareMode = TextCompare
            For i = LBound(ary_compare) To UBound(ary_compare)
                If Not .Exists(ary_compare(i, 1)) Then
                    .Add ary_compare(i, 1), Empty
                End If
            Next i
     End With
     
     'Compare and Add new key's and Items in dictionary
        With Dict2
             .CompareMode = TextCompare
            For i = LBound(ary_base) To UBound(ary_base)
                      If Not (dict.Exists(ary_base(i, 2))) Then
                           Dict2.Add (ary_base(i, 1)), (ary_base(i, 2))
                        End If
            Next i
        End With


Range("M2").Resize(Dict2.Count).Value = Application.Transpose(Dict2.Keys)
Range("N2").Resize(Dict2.Count).Value = Application.Transpose(Dict2.Items)

'Below code works
'For i = 0 To Dict2.Count - 1
'    Range("M" & 2 + i).Value = Dict2.Keys(i)
'    Range("N" & 2 + i).Value = Dict2.Items(i)
'
'Next i

End Sub


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,560
Members
449,089
Latest member
Motoracer88

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