Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wanted to test my skills a little with this, even though @matzone gave exact answer already. I made this Sub which does exactly the same, but using <code>Range</code> objects and <code>.Find()</code> method. With comments...</p> <pre><code>Private Sub Test() FindAndColorMatchesOfTwoColumns "A", "B" End Sub Private Sub FindAndColorMatchesOfTwoColumns(colTarget As String, colList As String) Dim rLookUp As Range ' Column range for list compared against Dim rSearchList As Range ' Column range for compare items Dim rMatch As Range Dim sAddress As String ' Set compared against list from colTarget column Set rLookUp = Range(colTarget &amp; "1:" &amp; _ colTarget &amp; Range(colTarget &amp; "1").End(xlDown).Row) ' Loop trough list from colList column For Each rSearchList In Range(colList &amp; "1:" &amp; colList &amp; Range(colList &amp; "1").End(xlDown).Row) ' Find for a match Set rMatch = rLookUp.Find(rSearchList.Value, LookAt:=xlWhole) If Not rMatch Is Nothing Then ' Store first address found sAddress = rMatch.Address ' Loop trough all matches using .FindNext, ' exit if found nothing or address is first found Do ' Set the color rMatch.Font.Color = vbRed Set rMatch = rLookUp.FindNext(rMatch) Loop While Not rMatch Is Nothing And rMatch.Address &lt;&gt; sAddress End If Next Set rMatch = Nothing Set rSearchList = Nothing Set rLookUp = Nothing End Sub </code></pre> <p>The idea is to be more dynamic, accept the two columns as arguments, set search ranges until <code>Range.End(xlDown).Row</code> and not fixed counts. Also loop trough only matches.</p> <p>For original question the simple <code>.Cells()</code> nested loops is way simpler, but using <code>.Find()</code> would prove alot faster if column counts would go up to thousand(s).</p> <p>Tested the "long list" hypothesis with this test sub:</p> <pre><code>Private Sub RunTest() Dim tStart As Date Dim tEnd As Date tStart = Timer FindAndColorMatchesOfTwoColumns "A", "B" tEnd = Timer Debug.Print Format(tEnd - tStart, "0.000") tStart = Timer Test tEnd = Timer Debug.Print Format(tEnd - tStart, "0.000") End Sub </code></pre> <p>Added 1500 rows to column A and 184 rows to column B and got <strong>Immediate</strong> view result as:</p> <pre><code>0,266 12,719 </code></pre> <p>So there indeed is a huge difference in performance... If OP was only providing simplistic example for question and intends to utilize this in larger sets of data.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload