Note that there are some explanatory texts on larger screens.

plurals
  1. POImproving efficiency with nested for loops & array comparisons
    primarykey
    data
    text
    <p>I have a macro that loops across two sheets comparing words in each cell. The code works fine, but is there a way to improve the efficiency or speed of this? I manually used a for loop to compare the string arrays as well, as I didn't find any VBA functions to do this. I do have ScreenUpdating off, which seems to help a bit.</p> <pre><code>For i = 2 To maxMn 'loop 1 Sheets("Sh1").Select Cells(i, 2).Select mnStr = Cells(i, 2).Value mnArr = Split(mnStr, " ") x = 2 For x = 2 To maxNm 'loop 2 numTotal = 0 numMatches = 0 Sheets("Sh2").Select Cells(x, 6).Select nameStr = Cells(x, 6).Value nameArr = Split(nameStr, " ") For Each mn In mnArr 'loop 3 For Each nam In nameArr 'loop 4 Application.StatusBar = "(#" &amp; i &amp; " Sh1) (#" &amp; x &amp; " Sh2): Comparing " &amp; mn &amp; " to " &amp; nam If LCase(nam) = LCase(mn) Then 'MsgBox "Yes, '" &amp; nam &amp; "' equal to " &amp; mn numMatches = numMatches + 1 Else 'MsgBox "No, '" &amp; nam &amp; "' does not equal " &amp; mn End If Next nam '4: For Each nam In nameArr numTotal = numTotal + 1 Next mn '3: For Each mn In mnArr If numTotal &gt; 2 And numTotal &gt; 0 And numMatches &gt;= numTotal / 2 Then LogMsg = "(#" &amp; i &amp; " Sh1) (#" &amp; x &amp; " Sh2): |" &amp; mnStr &amp; "| - |" &amp; nameStr &amp; "| = " &amp; numMatches &amp; "/" &amp; numTotal &amp; " matches." Print #FileNum, LogMsg 'MsgBox numMatches &amp; " matches out of " &amp; numTotal &amp; " total." End If Next x '2: For x = 2 To maxNm Next i '1: For i = 2 To maxMn </code></pre>
    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.
 

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