• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

help needed in understanding the code:

sreenivas

New Member
Code:
Sub kTest()
    Dim ka, k(), i As Long, n As Long
    ka = Range("a1").CurrentRegion.Resize(, 2)
    ReDim k(1 To UBound(ka, 1), 1 To 1)

    With CreateObject("scripting.dictionary")
         .comparemode = 1
         For i = 1 To UBound(ka, 1)
             .Item(ka(i, 2)) = Empty
         Next

         For i = 1 To UBound(ka, 1)
             If Not .exists(ka(i, 1)) Then
                n = n + 1
                k(n, 1) = ka(i, 1)
             End If
         Next
    End With

    If n Then
       Columns(1).ClearContents
       [a1].Resize(n).Value = k
    End If
End Sub

it is basically used to compare two column (A,B) and find the missing in column B and paste it in A. but i did not understand the code and i got this on Google.
 
Last edited by a moderator:
Hi Sreenivas

It basically just puts both lists into two arrays. Then it compares the arrays against one another. The compare mode =1 will only allow unique values so only values which are unique are allowed when the comparison is made.

In the procedure there are two variants - K and KA (well named variants I might add) and K is where the original data is stored and Ka is where the unique items in K are stored. When the loops are done the unique items in K are placed in Column A. It is no more complex than that.

To help yourself understand this code further put your cursor in the code and press F8 through each of the lines. You can look at the Locals window to see the value of the variables as they are assigned to each of the arrays. This will enable you to see what is happening.

Take care

Smallman
 
Last edited:
Back
Top