How can I compare two columns in Excel to highlight words that don't match?












2














(I'm using Microsoft excel 2010)



Lts say I have a list of phrases in both column A and column B (see screen shot below)



enter image description here



What I would like to happen whether it be with a macro, VBA or formula is:



If there is a word in any cell in column A that isn't any of the words in any cell in column B to highlight that word in red.



For example: in cell A9 the word "buy" is there, but the word buy isn't mentioned anywhere in column B so i would like the word buy to highlight in red.



How can I accomplish this?



(I think a macro/vba would be the best option but I have no idea how to create it, or even if its possible.)










share|improve this question




















  • 1




    You did a good job of explaining what you want. I edited it a little to add clarity to the title and remove the salutations (not used here). What would make the question better would be for you to edit your question to include what you've tried or researched.
    – CharlieRB
    Oct 24 '13 at 18:15








  • 1




    Is the word delimiter always a single space?
    – nixda
    Oct 24 '13 at 18:16


















2














(I'm using Microsoft excel 2010)



Lts say I have a list of phrases in both column A and column B (see screen shot below)



enter image description here



What I would like to happen whether it be with a macro, VBA or formula is:



If there is a word in any cell in column A that isn't any of the words in any cell in column B to highlight that word in red.



For example: in cell A9 the word "buy" is there, but the word buy isn't mentioned anywhere in column B so i would like the word buy to highlight in red.



How can I accomplish this?



(I think a macro/vba would be the best option but I have no idea how to create it, or even if its possible.)










share|improve this question




















  • 1




    You did a good job of explaining what you want. I edited it a little to add clarity to the title and remove the salutations (not used here). What would make the question better would be for you to edit your question to include what you've tried or researched.
    – CharlieRB
    Oct 24 '13 at 18:15








  • 1




    Is the word delimiter always a single space?
    – nixda
    Oct 24 '13 at 18:16
















2












2








2


1





(I'm using Microsoft excel 2010)



Lts say I have a list of phrases in both column A and column B (see screen shot below)



enter image description here



What I would like to happen whether it be with a macro, VBA or formula is:



If there is a word in any cell in column A that isn't any of the words in any cell in column B to highlight that word in red.



For example: in cell A9 the word "buy" is there, but the word buy isn't mentioned anywhere in column B so i would like the word buy to highlight in red.



How can I accomplish this?



(I think a macro/vba would be the best option but I have no idea how to create it, or even if its possible.)










share|improve this question















(I'm using Microsoft excel 2010)



Lts say I have a list of phrases in both column A and column B (see screen shot below)



enter image description here



What I would like to happen whether it be with a macro, VBA or formula is:



If there is a word in any cell in column A that isn't any of the words in any cell in column B to highlight that word in red.



For example: in cell A9 the word "buy" is there, but the word buy isn't mentioned anywhere in column B so i would like the word buy to highlight in red.



How can I accomplish this?



(I think a macro/vba would be the best option but I have no idea how to create it, or even if its possible.)







microsoft-excel microsoft-excel-2010 vba macros






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 14 at 21:01









Dave

23.2k74362




23.2k74362










asked Oct 24 '13 at 17:45









Jez Vander Brown

77313




77313








  • 1




    You did a good job of explaining what you want. I edited it a little to add clarity to the title and remove the salutations (not used here). What would make the question better would be for you to edit your question to include what you've tried or researched.
    – CharlieRB
    Oct 24 '13 at 18:15








  • 1




    Is the word delimiter always a single space?
    – nixda
    Oct 24 '13 at 18:16
















  • 1




    You did a good job of explaining what you want. I edited it a little to add clarity to the title and remove the salutations (not used here). What would make the question better would be for you to edit your question to include what you've tried or researched.
    – CharlieRB
    Oct 24 '13 at 18:15








  • 1




    Is the word delimiter always a single space?
    – nixda
    Oct 24 '13 at 18:16










1




1




You did a good job of explaining what you want. I edited it a little to add clarity to the title and remove the salutations (not used here). What would make the question better would be for you to edit your question to include what you've tried or researched.
– CharlieRB
Oct 24 '13 at 18:15






You did a good job of explaining what you want. I edited it a little to add clarity to the title and remove the salutations (not used here). What would make the question better would be for you to edit your question to include what you've tried or researched.
– CharlieRB
Oct 24 '13 at 18:15






1




1




Is the word delimiter always a single space?
– nixda
Oct 24 '13 at 18:16






Is the word delimiter always a single space?
– nixda
Oct 24 '13 at 18:16












2 Answers
2






active

oldest

votes


















2














Insert the following code into a VBA module.



Sub highlightWords()
Application.ScreenUpdating = False
Dim rng2HL As Range, rngCheck As Range, dictWords As Object
Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long
Set r = Selection
'Change the addresses below to match your data.
Set rng2HL = Range("A1:A9")
Set rngCheck = Range("B1:B9")
a = rng2HL.Value
b = rngCheck.Value
Set dictWords = CreateObject("Scripting.Dictionary")
'Load unique words from second column into a dictionary for easy checking
For i = LBound(b, 1) To UBound(b, 1)
wordlist = Split(b(i, 1), " ")
For j = LBound(wordlist) To UBound(wordlist)
If Not dictWords.Exists(wordlist(j)) Then
dictWords.Add wordlist(j), wordlist(j)
End If
Next j
Next i
'Reset range to highlight to all black font.
rng2HL.Font.ColorIndex = 1
'Check words one by one against dictionary.
For i = LBound(a, 1) To UBound(a, 1)
wordlist = Split(a(i, 1), " ")
For j = LBound(wordlist) To UBound(wordlist)
If Not dictWords.Exists(wordlist(j)) Then
wordStart = InStr(a(i, 1), wordlist(j))
'Change font color of word to red.
rng2HL.Cells(i).Characters(wordStart, Len(wordlist(j))).Font.ColorIndex = 3
End If
Next j
Next i
Application.ScreenUpdating = True
End Sub


Just be sure to change the addresses in the lines below to match your worksheet.



Set rng2HL = Range("A1:A9")
Set rngCheck = Range("B1:B9")


Results:



enter image description here



EDIT:



Since you added requirements in the comments below, I modified the code to also print out the list of red-highlighted phrases in column C. If you want this list elsewhere, you'll have to adjust the address in the last section of the code. I also improved the highlighting code -- I noticed that it would do weird things like only highlight the first instance of a non-matching word.



Sub highlightWords()
Application.ScreenUpdating = False
Dim rng2HL As Range, rngCheck As Range, dictWords As Object, dictRed As Object
Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long, phraseLen As Integer
Dim re As Object, consec As Integer, tmpPhrase As String
'Change the addresses below to match your data.
Set rng2HL = Range("A1:A9")
Set rngCheck = Range("B1:B9")
a = rng2HL.Value
b = rngCheck.Value
Set dictWords = CreateObject("Scripting.Dictionary")
'Load unique words from second column into a dictionary for easy checking
For i = LBound(b, 1) To UBound(b, 1)
wordlist = Split(b(i, 1), " ")
For j = LBound(wordlist) To UBound(wordlist)
If Not dictWords.Exists(wordlist(j)) Then
dictWords.Add wordlist(j), wordlist(j)
End If
Next j
Next i
Erase b
'Reset range to highlight to all black font.
rng2HL.Font.ColorIndex = 1
Set dictRed = CreateObject("Scripting.Dictionary")
Set re = CreateObject("vbscript.regexp")
'Check words one by one against dictionary.
For i = LBound(a, 1) To UBound(a, 1)
wordlist = Split(a(i, 1), " ")
consec = 0
tmpPhrase = ""
For j = LBound(wordlist) To UBound(wordlist)
If Not dictWords.Exists(wordlist(j)) Then
consec = consec + 1
If consec > 1 Then tmpPhrase = tmpPhrase & " "
tmpPhrase = tmpPhrase & wordlist(j)
Else
If consec > 0 Then
If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
re.Pattern = "(^| )" & tmpPhrase & "( |$)"
Set matches = re.Execute(a(i, 1))
For Each m In matches
wordStart = m.FirstIndex
phraseLen = m.Length
'Change font color of word to red.
rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
Next m
consec = 0
tmpPhrase = ""
End If
End If
Next j
'Highlight any matches that appear at the end of the line
If consec > 0 Then
If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
re.Pattern = "(^" & tmpPhrase & "| " & tmpPhrase & ")( |$)"
Set matches = re.Execute(a(i, 1))
For Each m In matches
wordStart = m.FirstIndex
phraseLen = m.Length
'Change font color of word to red.
rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
Next m
End If
Next i
Erase a
'Output list of unique red phrases to column C.
redkeys = dictRed.Keys
For k = LBound(redkeys) To UBound(redkeys)
Range("C1").Offset(k, 0).Value = redkeys(k)
Next k
Erase redkeys
Application.ScreenUpdating = True
End Sub


new example






share|improve this answer























  • This is perfect!! thank you so much for your help, i didn't think it would be possible. Now that i know it is could you maybe help a little further and include in the macro; Once the words are highlighted in red to move those words into column C as a list? And if you could go one step further and also include once the list is in column C to remove any duplicate words?
    – Jez Vander Brown
    Oct 25 '13 at 8:10












  • If this can't be included in the same VBA would you be able to create a separate one for me?
    – Jez Vander Brown
    Oct 25 '13 at 8:19










  • @JezVanderBrown OK, I added new code to my answer.
    – Excellll
    Oct 29 '13 at 21:25



















0














If you put A and B on separate sheets then you can use Text to Columns to split each item up into multiple cells, one word per cell. Then a simple LOOKUP() would let you find words that don't appear in the other set of cells.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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%2fsuperuser.com%2fquestions%2f664926%2fhow-can-i-compare-two-columns-in-excel-to-highlight-words-that-dont-match%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Insert the following code into a VBA module.



    Sub highlightWords()
    Application.ScreenUpdating = False
    Dim rng2HL As Range, rngCheck As Range, dictWords As Object
    Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long
    Set r = Selection
    'Change the addresses below to match your data.
    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")
    a = rng2HL.Value
    b = rngCheck.Value
    Set dictWords = CreateObject("Scripting.Dictionary")
    'Load unique words from second column into a dictionary for easy checking
    For i = LBound(b, 1) To UBound(b, 1)
    wordlist = Split(b(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    dictWords.Add wordlist(j), wordlist(j)
    End If
    Next j
    Next i
    'Reset range to highlight to all black font.
    rng2HL.Font.ColorIndex = 1
    'Check words one by one against dictionary.
    For i = LBound(a, 1) To UBound(a, 1)
    wordlist = Split(a(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    wordStart = InStr(a(i, 1), wordlist(j))
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart, Len(wordlist(j))).Font.ColorIndex = 3
    End If
    Next j
    Next i
    Application.ScreenUpdating = True
    End Sub


    Just be sure to change the addresses in the lines below to match your worksheet.



    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")


    Results:



    enter image description here



    EDIT:



    Since you added requirements in the comments below, I modified the code to also print out the list of red-highlighted phrases in column C. If you want this list elsewhere, you'll have to adjust the address in the last section of the code. I also improved the highlighting code -- I noticed that it would do weird things like only highlight the first instance of a non-matching word.



    Sub highlightWords()
    Application.ScreenUpdating = False
    Dim rng2HL As Range, rngCheck As Range, dictWords As Object, dictRed As Object
    Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long, phraseLen As Integer
    Dim re As Object, consec As Integer, tmpPhrase As String
    'Change the addresses below to match your data.
    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")
    a = rng2HL.Value
    b = rngCheck.Value
    Set dictWords = CreateObject("Scripting.Dictionary")
    'Load unique words from second column into a dictionary for easy checking
    For i = LBound(b, 1) To UBound(b, 1)
    wordlist = Split(b(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    dictWords.Add wordlist(j), wordlist(j)
    End If
    Next j
    Next i
    Erase b
    'Reset range to highlight to all black font.
    rng2HL.Font.ColorIndex = 1
    Set dictRed = CreateObject("Scripting.Dictionary")
    Set re = CreateObject("vbscript.regexp")
    'Check words one by one against dictionary.
    For i = LBound(a, 1) To UBound(a, 1)
    wordlist = Split(a(i, 1), " ")
    consec = 0
    tmpPhrase = ""
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    consec = consec + 1
    If consec > 1 Then tmpPhrase = tmpPhrase & " "
    tmpPhrase = tmpPhrase & wordlist(j)
    Else
    If consec > 0 Then
    If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
    re.Pattern = "(^| )" & tmpPhrase & "( |$)"
    Set matches = re.Execute(a(i, 1))
    For Each m In matches
    wordStart = m.FirstIndex
    phraseLen = m.Length
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
    Next m
    consec = 0
    tmpPhrase = ""
    End If
    End If
    Next j
    'Highlight any matches that appear at the end of the line
    If consec > 0 Then
    If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
    re.Pattern = "(^" & tmpPhrase & "| " & tmpPhrase & ")( |$)"
    Set matches = re.Execute(a(i, 1))
    For Each m In matches
    wordStart = m.FirstIndex
    phraseLen = m.Length
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
    Next m
    End If
    Next i
    Erase a
    'Output list of unique red phrases to column C.
    redkeys = dictRed.Keys
    For k = LBound(redkeys) To UBound(redkeys)
    Range("C1").Offset(k, 0).Value = redkeys(k)
    Next k
    Erase redkeys
    Application.ScreenUpdating = True
    End Sub


    new example






    share|improve this answer























    • This is perfect!! thank you so much for your help, i didn't think it would be possible. Now that i know it is could you maybe help a little further and include in the macro; Once the words are highlighted in red to move those words into column C as a list? And if you could go one step further and also include once the list is in column C to remove any duplicate words?
      – Jez Vander Brown
      Oct 25 '13 at 8:10












    • If this can't be included in the same VBA would you be able to create a separate one for me?
      – Jez Vander Brown
      Oct 25 '13 at 8:19










    • @JezVanderBrown OK, I added new code to my answer.
      – Excellll
      Oct 29 '13 at 21:25
















    2














    Insert the following code into a VBA module.



    Sub highlightWords()
    Application.ScreenUpdating = False
    Dim rng2HL As Range, rngCheck As Range, dictWords As Object
    Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long
    Set r = Selection
    'Change the addresses below to match your data.
    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")
    a = rng2HL.Value
    b = rngCheck.Value
    Set dictWords = CreateObject("Scripting.Dictionary")
    'Load unique words from second column into a dictionary for easy checking
    For i = LBound(b, 1) To UBound(b, 1)
    wordlist = Split(b(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    dictWords.Add wordlist(j), wordlist(j)
    End If
    Next j
    Next i
    'Reset range to highlight to all black font.
    rng2HL.Font.ColorIndex = 1
    'Check words one by one against dictionary.
    For i = LBound(a, 1) To UBound(a, 1)
    wordlist = Split(a(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    wordStart = InStr(a(i, 1), wordlist(j))
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart, Len(wordlist(j))).Font.ColorIndex = 3
    End If
    Next j
    Next i
    Application.ScreenUpdating = True
    End Sub


    Just be sure to change the addresses in the lines below to match your worksheet.



    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")


    Results:



    enter image description here



    EDIT:



    Since you added requirements in the comments below, I modified the code to also print out the list of red-highlighted phrases in column C. If you want this list elsewhere, you'll have to adjust the address in the last section of the code. I also improved the highlighting code -- I noticed that it would do weird things like only highlight the first instance of a non-matching word.



    Sub highlightWords()
    Application.ScreenUpdating = False
    Dim rng2HL As Range, rngCheck As Range, dictWords As Object, dictRed As Object
    Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long, phraseLen As Integer
    Dim re As Object, consec As Integer, tmpPhrase As String
    'Change the addresses below to match your data.
    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")
    a = rng2HL.Value
    b = rngCheck.Value
    Set dictWords = CreateObject("Scripting.Dictionary")
    'Load unique words from second column into a dictionary for easy checking
    For i = LBound(b, 1) To UBound(b, 1)
    wordlist = Split(b(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    dictWords.Add wordlist(j), wordlist(j)
    End If
    Next j
    Next i
    Erase b
    'Reset range to highlight to all black font.
    rng2HL.Font.ColorIndex = 1
    Set dictRed = CreateObject("Scripting.Dictionary")
    Set re = CreateObject("vbscript.regexp")
    'Check words one by one against dictionary.
    For i = LBound(a, 1) To UBound(a, 1)
    wordlist = Split(a(i, 1), " ")
    consec = 0
    tmpPhrase = ""
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    consec = consec + 1
    If consec > 1 Then tmpPhrase = tmpPhrase & " "
    tmpPhrase = tmpPhrase & wordlist(j)
    Else
    If consec > 0 Then
    If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
    re.Pattern = "(^| )" & tmpPhrase & "( |$)"
    Set matches = re.Execute(a(i, 1))
    For Each m In matches
    wordStart = m.FirstIndex
    phraseLen = m.Length
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
    Next m
    consec = 0
    tmpPhrase = ""
    End If
    End If
    Next j
    'Highlight any matches that appear at the end of the line
    If consec > 0 Then
    If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
    re.Pattern = "(^" & tmpPhrase & "| " & tmpPhrase & ")( |$)"
    Set matches = re.Execute(a(i, 1))
    For Each m In matches
    wordStart = m.FirstIndex
    phraseLen = m.Length
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
    Next m
    End If
    Next i
    Erase a
    'Output list of unique red phrases to column C.
    redkeys = dictRed.Keys
    For k = LBound(redkeys) To UBound(redkeys)
    Range("C1").Offset(k, 0).Value = redkeys(k)
    Next k
    Erase redkeys
    Application.ScreenUpdating = True
    End Sub


    new example






    share|improve this answer























    • This is perfect!! thank you so much for your help, i didn't think it would be possible. Now that i know it is could you maybe help a little further and include in the macro; Once the words are highlighted in red to move those words into column C as a list? And if you could go one step further and also include once the list is in column C to remove any duplicate words?
      – Jez Vander Brown
      Oct 25 '13 at 8:10












    • If this can't be included in the same VBA would you be able to create a separate one for me?
      – Jez Vander Brown
      Oct 25 '13 at 8:19










    • @JezVanderBrown OK, I added new code to my answer.
      – Excellll
      Oct 29 '13 at 21:25














    2












    2








    2






    Insert the following code into a VBA module.



    Sub highlightWords()
    Application.ScreenUpdating = False
    Dim rng2HL As Range, rngCheck As Range, dictWords As Object
    Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long
    Set r = Selection
    'Change the addresses below to match your data.
    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")
    a = rng2HL.Value
    b = rngCheck.Value
    Set dictWords = CreateObject("Scripting.Dictionary")
    'Load unique words from second column into a dictionary for easy checking
    For i = LBound(b, 1) To UBound(b, 1)
    wordlist = Split(b(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    dictWords.Add wordlist(j), wordlist(j)
    End If
    Next j
    Next i
    'Reset range to highlight to all black font.
    rng2HL.Font.ColorIndex = 1
    'Check words one by one against dictionary.
    For i = LBound(a, 1) To UBound(a, 1)
    wordlist = Split(a(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    wordStart = InStr(a(i, 1), wordlist(j))
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart, Len(wordlist(j))).Font.ColorIndex = 3
    End If
    Next j
    Next i
    Application.ScreenUpdating = True
    End Sub


    Just be sure to change the addresses in the lines below to match your worksheet.



    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")


    Results:



    enter image description here



    EDIT:



    Since you added requirements in the comments below, I modified the code to also print out the list of red-highlighted phrases in column C. If you want this list elsewhere, you'll have to adjust the address in the last section of the code. I also improved the highlighting code -- I noticed that it would do weird things like only highlight the first instance of a non-matching word.



    Sub highlightWords()
    Application.ScreenUpdating = False
    Dim rng2HL As Range, rngCheck As Range, dictWords As Object, dictRed As Object
    Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long, phraseLen As Integer
    Dim re As Object, consec As Integer, tmpPhrase As String
    'Change the addresses below to match your data.
    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")
    a = rng2HL.Value
    b = rngCheck.Value
    Set dictWords = CreateObject("Scripting.Dictionary")
    'Load unique words from second column into a dictionary for easy checking
    For i = LBound(b, 1) To UBound(b, 1)
    wordlist = Split(b(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    dictWords.Add wordlist(j), wordlist(j)
    End If
    Next j
    Next i
    Erase b
    'Reset range to highlight to all black font.
    rng2HL.Font.ColorIndex = 1
    Set dictRed = CreateObject("Scripting.Dictionary")
    Set re = CreateObject("vbscript.regexp")
    'Check words one by one against dictionary.
    For i = LBound(a, 1) To UBound(a, 1)
    wordlist = Split(a(i, 1), " ")
    consec = 0
    tmpPhrase = ""
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    consec = consec + 1
    If consec > 1 Then tmpPhrase = tmpPhrase & " "
    tmpPhrase = tmpPhrase & wordlist(j)
    Else
    If consec > 0 Then
    If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
    re.Pattern = "(^| )" & tmpPhrase & "( |$)"
    Set matches = re.Execute(a(i, 1))
    For Each m In matches
    wordStart = m.FirstIndex
    phraseLen = m.Length
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
    Next m
    consec = 0
    tmpPhrase = ""
    End If
    End If
    Next j
    'Highlight any matches that appear at the end of the line
    If consec > 0 Then
    If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
    re.Pattern = "(^" & tmpPhrase & "| " & tmpPhrase & ")( |$)"
    Set matches = re.Execute(a(i, 1))
    For Each m In matches
    wordStart = m.FirstIndex
    phraseLen = m.Length
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
    Next m
    End If
    Next i
    Erase a
    'Output list of unique red phrases to column C.
    redkeys = dictRed.Keys
    For k = LBound(redkeys) To UBound(redkeys)
    Range("C1").Offset(k, 0).Value = redkeys(k)
    Next k
    Erase redkeys
    Application.ScreenUpdating = True
    End Sub


    new example






    share|improve this answer














    Insert the following code into a VBA module.



    Sub highlightWords()
    Application.ScreenUpdating = False
    Dim rng2HL As Range, rngCheck As Range, dictWords As Object
    Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long
    Set r = Selection
    'Change the addresses below to match your data.
    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")
    a = rng2HL.Value
    b = rngCheck.Value
    Set dictWords = CreateObject("Scripting.Dictionary")
    'Load unique words from second column into a dictionary for easy checking
    For i = LBound(b, 1) To UBound(b, 1)
    wordlist = Split(b(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    dictWords.Add wordlist(j), wordlist(j)
    End If
    Next j
    Next i
    'Reset range to highlight to all black font.
    rng2HL.Font.ColorIndex = 1
    'Check words one by one against dictionary.
    For i = LBound(a, 1) To UBound(a, 1)
    wordlist = Split(a(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    wordStart = InStr(a(i, 1), wordlist(j))
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart, Len(wordlist(j))).Font.ColorIndex = 3
    End If
    Next j
    Next i
    Application.ScreenUpdating = True
    End Sub


    Just be sure to change the addresses in the lines below to match your worksheet.



    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")


    Results:



    enter image description here



    EDIT:



    Since you added requirements in the comments below, I modified the code to also print out the list of red-highlighted phrases in column C. If you want this list elsewhere, you'll have to adjust the address in the last section of the code. I also improved the highlighting code -- I noticed that it would do weird things like only highlight the first instance of a non-matching word.



    Sub highlightWords()
    Application.ScreenUpdating = False
    Dim rng2HL As Range, rngCheck As Range, dictWords As Object, dictRed As Object
    Dim a() As Variant, b() As Variant, wordlist As Variant, wordStart As Long, phraseLen As Integer
    Dim re As Object, consec As Integer, tmpPhrase As String
    'Change the addresses below to match your data.
    Set rng2HL = Range("A1:A9")
    Set rngCheck = Range("B1:B9")
    a = rng2HL.Value
    b = rngCheck.Value
    Set dictWords = CreateObject("Scripting.Dictionary")
    'Load unique words from second column into a dictionary for easy checking
    For i = LBound(b, 1) To UBound(b, 1)
    wordlist = Split(b(i, 1), " ")
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    dictWords.Add wordlist(j), wordlist(j)
    End If
    Next j
    Next i
    Erase b
    'Reset range to highlight to all black font.
    rng2HL.Font.ColorIndex = 1
    Set dictRed = CreateObject("Scripting.Dictionary")
    Set re = CreateObject("vbscript.regexp")
    'Check words one by one against dictionary.
    For i = LBound(a, 1) To UBound(a, 1)
    wordlist = Split(a(i, 1), " ")
    consec = 0
    tmpPhrase = ""
    For j = LBound(wordlist) To UBound(wordlist)
    If Not dictWords.Exists(wordlist(j)) Then
    consec = consec + 1
    If consec > 1 Then tmpPhrase = tmpPhrase & " "
    tmpPhrase = tmpPhrase & wordlist(j)
    Else
    If consec > 0 Then
    If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
    re.Pattern = "(^| )" & tmpPhrase & "( |$)"
    Set matches = re.Execute(a(i, 1))
    For Each m In matches
    wordStart = m.FirstIndex
    phraseLen = m.Length
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
    Next m
    consec = 0
    tmpPhrase = ""
    End If
    End If
    Next j
    'Highlight any matches that appear at the end of the line
    If consec > 0 Then
    If Not dictRed.Exists(tmpPhrase) Then dictRed.Add tmpPhrase, tmpPhrase
    re.Pattern = "(^" & tmpPhrase & "| " & tmpPhrase & ")( |$)"
    Set matches = re.Execute(a(i, 1))
    For Each m In matches
    wordStart = m.FirstIndex
    phraseLen = m.Length
    'Change font color of word to red.
    rng2HL.Cells(i).Characters(wordStart + 1, phraseLen).Font.ColorIndex = 3
    Next m
    End If
    Next i
    Erase a
    'Output list of unique red phrases to column C.
    redkeys = dictRed.Keys
    For k = LBound(redkeys) To UBound(redkeys)
    Range("C1").Offset(k, 0).Value = redkeys(k)
    Next k
    Erase redkeys
    Application.ScreenUpdating = True
    End Sub


    new example







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 29 '13 at 21:19

























    answered Oct 24 '13 at 20:24









    Excellll

    11k74162




    11k74162












    • This is perfect!! thank you so much for your help, i didn't think it would be possible. Now that i know it is could you maybe help a little further and include in the macro; Once the words are highlighted in red to move those words into column C as a list? And if you could go one step further and also include once the list is in column C to remove any duplicate words?
      – Jez Vander Brown
      Oct 25 '13 at 8:10












    • If this can't be included in the same VBA would you be able to create a separate one for me?
      – Jez Vander Brown
      Oct 25 '13 at 8:19










    • @JezVanderBrown OK, I added new code to my answer.
      – Excellll
      Oct 29 '13 at 21:25


















    • This is perfect!! thank you so much for your help, i didn't think it would be possible. Now that i know it is could you maybe help a little further and include in the macro; Once the words are highlighted in red to move those words into column C as a list? And if you could go one step further and also include once the list is in column C to remove any duplicate words?
      – Jez Vander Brown
      Oct 25 '13 at 8:10












    • If this can't be included in the same VBA would you be able to create a separate one for me?
      – Jez Vander Brown
      Oct 25 '13 at 8:19










    • @JezVanderBrown OK, I added new code to my answer.
      – Excellll
      Oct 29 '13 at 21:25
















    This is perfect!! thank you so much for your help, i didn't think it would be possible. Now that i know it is could you maybe help a little further and include in the macro; Once the words are highlighted in red to move those words into column C as a list? And if you could go one step further and also include once the list is in column C to remove any duplicate words?
    – Jez Vander Brown
    Oct 25 '13 at 8:10






    This is perfect!! thank you so much for your help, i didn't think it would be possible. Now that i know it is could you maybe help a little further and include in the macro; Once the words are highlighted in red to move those words into column C as a list? And if you could go one step further and also include once the list is in column C to remove any duplicate words?
    – Jez Vander Brown
    Oct 25 '13 at 8:10














    If this can't be included in the same VBA would you be able to create a separate one for me?
    – Jez Vander Brown
    Oct 25 '13 at 8:19




    If this can't be included in the same VBA would you be able to create a separate one for me?
    – Jez Vander Brown
    Oct 25 '13 at 8:19












    @JezVanderBrown OK, I added new code to my answer.
    – Excellll
    Oct 29 '13 at 21:25




    @JezVanderBrown OK, I added new code to my answer.
    – Excellll
    Oct 29 '13 at 21:25













    0














    If you put A and B on separate sheets then you can use Text to Columns to split each item up into multiple cells, one word per cell. Then a simple LOOKUP() would let you find words that don't appear in the other set of cells.






    share|improve this answer


























      0














      If you put A and B on separate sheets then you can use Text to Columns to split each item up into multiple cells, one word per cell. Then a simple LOOKUP() would let you find words that don't appear in the other set of cells.






      share|improve this answer
























        0












        0








        0






        If you put A and B on separate sheets then you can use Text to Columns to split each item up into multiple cells, one word per cell. Then a simple LOOKUP() would let you find words that don't appear in the other set of cells.






        share|improve this answer












        If you put A and B on separate sheets then you can use Text to Columns to split each item up into multiple cells, one word per cell. Then a simple LOOKUP() would let you find words that don't appear in the other set of cells.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 24 '13 at 20:43









        Sparr

        807917




        807917






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fsuperuser.com%2fquestions%2f664926%2fhow-can-i-compare-two-columns-in-excel-to-highlight-words-that-dont-match%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

            Index of /

            Tribalistas

            Listed building