compare column with unique identifier in vba excel










1















I need to compare the data with unique identifier which is the concatenation of (Column A, "~" Column B) and store it in Column F. Find all the duplicate values in ColumnF, which will used as a basis for comparing to the other Columns (Column C, Column D and Column E). For example,



enter image description here



In my example, I have a duplicate value of 5*2018~OPS$CABUCKLE, in this case I will compare each column using the identifier. In my 1st entry, Column C have the same value in 2nd entry which is 222, but in Column D the value of 1st entry is N and it was changed to Y in 2nd entry. Same case in Column E. I need to highlight the changes happened between the entries.



I only did the concatenation in VBA, but I don't know how will I find the duplicate value and compare the other column?



Sub split1()
Dim ws As Worksheet, lRow As Long
Dim x As Long

Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

With ws
For x = 1 To lRow
For Each wrd In .Cells(x, 1)
d = wrd
For Each nm In .Cells(x, 2)
.Cells(x, 6).Value = d & "*" & nm
Next nm
Next
Next x
End With
End Sub









share|improve this question
























  • I believe this could be done with conditional formatting -- do you absolutely need a VBA solution?

    – cybernetic.nomad
    Nov 15 '18 at 15:03











  • yes, it can be done using conditional formatting, but I don't know how will I do that.

    – pinkpanther
    Nov 15 '18 at 15:04











  • @pinkpanther can each only change once, like your example Y to N or N to Y? Or can they change multiple times switching between Y and N

    – Kubie
    Nov 15 '18 at 15:39















1















I need to compare the data with unique identifier which is the concatenation of (Column A, "~" Column B) and store it in Column F. Find all the duplicate values in ColumnF, which will used as a basis for comparing to the other Columns (Column C, Column D and Column E). For example,



enter image description here



In my example, I have a duplicate value of 5*2018~OPS$CABUCKLE, in this case I will compare each column using the identifier. In my 1st entry, Column C have the same value in 2nd entry which is 222, but in Column D the value of 1st entry is N and it was changed to Y in 2nd entry. Same case in Column E. I need to highlight the changes happened between the entries.



I only did the concatenation in VBA, but I don't know how will I find the duplicate value and compare the other column?



Sub split1()
Dim ws As Worksheet, lRow As Long
Dim x As Long

Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

With ws
For x = 1 To lRow
For Each wrd In .Cells(x, 1)
d = wrd
For Each nm In .Cells(x, 2)
.Cells(x, 6).Value = d & "*" & nm
Next nm
Next
Next x
End With
End Sub









share|improve this question
























  • I believe this could be done with conditional formatting -- do you absolutely need a VBA solution?

    – cybernetic.nomad
    Nov 15 '18 at 15:03











  • yes, it can be done using conditional formatting, but I don't know how will I do that.

    – pinkpanther
    Nov 15 '18 at 15:04











  • @pinkpanther can each only change once, like your example Y to N or N to Y? Or can they change multiple times switching between Y and N

    – Kubie
    Nov 15 '18 at 15:39













1












1








1








I need to compare the data with unique identifier which is the concatenation of (Column A, "~" Column B) and store it in Column F. Find all the duplicate values in ColumnF, which will used as a basis for comparing to the other Columns (Column C, Column D and Column E). For example,



enter image description here



In my example, I have a duplicate value of 5*2018~OPS$CABUCKLE, in this case I will compare each column using the identifier. In my 1st entry, Column C have the same value in 2nd entry which is 222, but in Column D the value of 1st entry is N and it was changed to Y in 2nd entry. Same case in Column E. I need to highlight the changes happened between the entries.



I only did the concatenation in VBA, but I don't know how will I find the duplicate value and compare the other column?



Sub split1()
Dim ws As Worksheet, lRow As Long
Dim x As Long

Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

With ws
For x = 1 To lRow
For Each wrd In .Cells(x, 1)
d = wrd
For Each nm In .Cells(x, 2)
.Cells(x, 6).Value = d & "*" & nm
Next nm
Next
Next x
End With
End Sub









share|improve this question
















I need to compare the data with unique identifier which is the concatenation of (Column A, "~" Column B) and store it in Column F. Find all the duplicate values in ColumnF, which will used as a basis for comparing to the other Columns (Column C, Column D and Column E). For example,



enter image description here



In my example, I have a duplicate value of 5*2018~OPS$CABUCKLE, in this case I will compare each column using the identifier. In my 1st entry, Column C have the same value in 2nd entry which is 222, but in Column D the value of 1st entry is N and it was changed to Y in 2nd entry. Same case in Column E. I need to highlight the changes happened between the entries.



I only did the concatenation in VBA, but I don't know how will I find the duplicate value and compare the other column?



Sub split1()
Dim ws As Worksheet, lRow As Long
Dim x As Long

Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

With ws
For x = 1 To lRow
For Each wrd In .Cells(x, 1)
d = wrd
For Each nm In .Cells(x, 2)
.Cells(x, 6).Value = d & "*" & nm
Next nm
Next
Next x
End With
End Sub






excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:21









Cindy Meister

15.5k102236




15.5k102236










asked Nov 15 '18 at 14:58









pinkpantherpinkpanther

556




556












  • I believe this could be done with conditional formatting -- do you absolutely need a VBA solution?

    – cybernetic.nomad
    Nov 15 '18 at 15:03











  • yes, it can be done using conditional formatting, but I don't know how will I do that.

    – pinkpanther
    Nov 15 '18 at 15:04











  • @pinkpanther can each only change once, like your example Y to N or N to Y? Or can they change multiple times switching between Y and N

    – Kubie
    Nov 15 '18 at 15:39

















  • I believe this could be done with conditional formatting -- do you absolutely need a VBA solution?

    – cybernetic.nomad
    Nov 15 '18 at 15:03











  • yes, it can be done using conditional formatting, but I don't know how will I do that.

    – pinkpanther
    Nov 15 '18 at 15:04











  • @pinkpanther can each only change once, like your example Y to N or N to Y? Or can they change multiple times switching between Y and N

    – Kubie
    Nov 15 '18 at 15:39
















I believe this could be done with conditional formatting -- do you absolutely need a VBA solution?

– cybernetic.nomad
Nov 15 '18 at 15:03





I believe this could be done with conditional formatting -- do you absolutely need a VBA solution?

– cybernetic.nomad
Nov 15 '18 at 15:03













yes, it can be done using conditional formatting, but I don't know how will I do that.

– pinkpanther
Nov 15 '18 at 15:04





yes, it can be done using conditional formatting, but I don't know how will I do that.

– pinkpanther
Nov 15 '18 at 15:04













@pinkpanther can each only change once, like your example Y to N or N to Y? Or can they change multiple times switching between Y and N

– Kubie
Nov 15 '18 at 15:39





@pinkpanther can each only change once, like your example Y to N or N to Y? Or can they change multiple times switching between Y and N

– Kubie
Nov 15 '18 at 15:39












1 Answer
1






active

oldest

votes


















0














This could achieve what you're looking for, let me know if it misses anything



Just don't forget to go to Tools > References and check 'Microsoft Scripting Runtime'



Sub highlight()

' need to include Microsoft Scripting Runtime in Tools > References
Dim prevIDs As Scripting.Dictionary: Set prevIDs = New Scripting.Dictionary
Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
Dim lastRow As Long
Dim oldRow As Long
Dim row As Long
Dim id As String

With ws
lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
For row = 2 To lastRow
' set lookup value
.Cells(row, "F").Value = Trim(CStr(.Cells(row, "A").Value)) & "~" & Trim(CStr(.Cells(row, "B").Value))
id = .Cells(row, "F").Value
If prevIDs.Exists(id) Then
' get previously found row
oldRow = prevIDs(id)
If .Cells(row, "C").Value = .Cells(oldRow, "C").Value Then
' only checks if col D doesn't match -- can change to check both
If .Cells(row, "D").Value <> .Cells(oldRow, "D").Value Then
.Range("D" & row & ":E" & row).Interior.Color = RGB(100, 200, 100)
.Range("D" & oldRow & ":E" & oldRow).Interior.Color = RGB(100, 200, 100)
End If
End If
' reset last found row
prevIDs(id) = row
Else
prevIDs.Add id, row
End If
Next
End With
End Sub


Here's my test:



enter image description here






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322212%2fcompare-column-with-unique-identifier-in-vba-excel%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This could achieve what you're looking for, let me know if it misses anything



    Just don't forget to go to Tools > References and check 'Microsoft Scripting Runtime'



    Sub highlight()

    ' need to include Microsoft Scripting Runtime in Tools > References
    Dim prevIDs As Scripting.Dictionary: Set prevIDs = New Scripting.Dictionary
    Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
    Dim lastRow As Long
    Dim oldRow As Long
    Dim row As Long
    Dim id As String

    With ws
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
    For row = 2 To lastRow
    ' set lookup value
    .Cells(row, "F").Value = Trim(CStr(.Cells(row, "A").Value)) & "~" & Trim(CStr(.Cells(row, "B").Value))
    id = .Cells(row, "F").Value
    If prevIDs.Exists(id) Then
    ' get previously found row
    oldRow = prevIDs(id)
    If .Cells(row, "C").Value = .Cells(oldRow, "C").Value Then
    ' only checks if col D doesn't match -- can change to check both
    If .Cells(row, "D").Value <> .Cells(oldRow, "D").Value Then
    .Range("D" & row & ":E" & row).Interior.Color = RGB(100, 200, 100)
    .Range("D" & oldRow & ":E" & oldRow).Interior.Color = RGB(100, 200, 100)
    End If
    End If
    ' reset last found row
    prevIDs(id) = row
    Else
    prevIDs.Add id, row
    End If
    Next
    End With
    End Sub


    Here's my test:



    enter image description here






    share|improve this answer





























      0














      This could achieve what you're looking for, let me know if it misses anything



      Just don't forget to go to Tools > References and check 'Microsoft Scripting Runtime'



      Sub highlight()

      ' need to include Microsoft Scripting Runtime in Tools > References
      Dim prevIDs As Scripting.Dictionary: Set prevIDs = New Scripting.Dictionary
      Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
      Dim lastRow As Long
      Dim oldRow As Long
      Dim row As Long
      Dim id As String

      With ws
      lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
      For row = 2 To lastRow
      ' set lookup value
      .Cells(row, "F").Value = Trim(CStr(.Cells(row, "A").Value)) & "~" & Trim(CStr(.Cells(row, "B").Value))
      id = .Cells(row, "F").Value
      If prevIDs.Exists(id) Then
      ' get previously found row
      oldRow = prevIDs(id)
      If .Cells(row, "C").Value = .Cells(oldRow, "C").Value Then
      ' only checks if col D doesn't match -- can change to check both
      If .Cells(row, "D").Value <> .Cells(oldRow, "D").Value Then
      .Range("D" & row & ":E" & row).Interior.Color = RGB(100, 200, 100)
      .Range("D" & oldRow & ":E" & oldRow).Interior.Color = RGB(100, 200, 100)
      End If
      End If
      ' reset last found row
      prevIDs(id) = row
      Else
      prevIDs.Add id, row
      End If
      Next
      End With
      End Sub


      Here's my test:



      enter image description here






      share|improve this answer



























        0












        0








        0







        This could achieve what you're looking for, let me know if it misses anything



        Just don't forget to go to Tools > References and check 'Microsoft Scripting Runtime'



        Sub highlight()

        ' need to include Microsoft Scripting Runtime in Tools > References
        Dim prevIDs As Scripting.Dictionary: Set prevIDs = New Scripting.Dictionary
        Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
        Dim lastRow As Long
        Dim oldRow As Long
        Dim row As Long
        Dim id As String

        With ws
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
        For row = 2 To lastRow
        ' set lookup value
        .Cells(row, "F").Value = Trim(CStr(.Cells(row, "A").Value)) & "~" & Trim(CStr(.Cells(row, "B").Value))
        id = .Cells(row, "F").Value
        If prevIDs.Exists(id) Then
        ' get previously found row
        oldRow = prevIDs(id)
        If .Cells(row, "C").Value = .Cells(oldRow, "C").Value Then
        ' only checks if col D doesn't match -- can change to check both
        If .Cells(row, "D").Value <> .Cells(oldRow, "D").Value Then
        .Range("D" & row & ":E" & row).Interior.Color = RGB(100, 200, 100)
        .Range("D" & oldRow & ":E" & oldRow).Interior.Color = RGB(100, 200, 100)
        End If
        End If
        ' reset last found row
        prevIDs(id) = row
        Else
        prevIDs.Add id, row
        End If
        Next
        End With
        End Sub


        Here's my test:



        enter image description here






        share|improve this answer















        This could achieve what you're looking for, let me know if it misses anything



        Just don't forget to go to Tools > References and check 'Microsoft Scripting Runtime'



        Sub highlight()

        ' need to include Microsoft Scripting Runtime in Tools > References
        Dim prevIDs As Scripting.Dictionary: Set prevIDs = New Scripting.Dictionary
        Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
        Dim lastRow As Long
        Dim oldRow As Long
        Dim row As Long
        Dim id As String

        With ws
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
        For row = 2 To lastRow
        ' set lookup value
        .Cells(row, "F").Value = Trim(CStr(.Cells(row, "A").Value)) & "~" & Trim(CStr(.Cells(row, "B").Value))
        id = .Cells(row, "F").Value
        If prevIDs.Exists(id) Then
        ' get previously found row
        oldRow = prevIDs(id)
        If .Cells(row, "C").Value = .Cells(oldRow, "C").Value Then
        ' only checks if col D doesn't match -- can change to check both
        If .Cells(row, "D").Value <> .Cells(oldRow, "D").Value Then
        .Range("D" & row & ":E" & row).Interior.Color = RGB(100, 200, 100)
        .Range("D" & oldRow & ":E" & oldRow).Interior.Color = RGB(100, 200, 100)
        End If
        End If
        ' reset last found row
        prevIDs(id) = row
        Else
        prevIDs.Add id, row
        End If
        Next
        End With
        End Sub


        Here's my test:



        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 16:57

























        answered Nov 15 '18 at 15:57









        KubieKubie

        1,3521518




        1,3521518





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322212%2fcompare-column-with-unique-identifier-in-vba-excel%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線