Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first rule of improving efficiency is don't select or activate anything. With datasets of 300 and 200 rows respectively, your code took 13.5 minutes. Just removing the selects</p> <pre><code> For i = 2 To maxMn 'loop 1 'Sheets("Sh1").Select 'Cells(i, 2).Select mnStr = Sheets("Sh1").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 = Sheets("Sh2").Cells(x, 6).Value </code></pre> <p>cut the time to 154 seconds. The screen redrawing is the single biggest time sink. The below code runs in 2.18 seconds (5.6 seconds if you add a statusbar update - which you don't need if it only takes 2 seconds)</p> <pre><code>Sub CompareWords2() Dim vaNam As Variant, vaMn As Variant Dim i As Long, j As Long Dim vaSplitNam As Variant, vaSplitMn As Variant Dim colUnique As Collection Dim lWord As Long Dim sLog As String Dim lMatches As Long, lTotal As Long Dim sgStart As Single sgStart = Timer 'Put both ranges in an array With ThisWorkbook.Sheets("Sh1") vaMn = .Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp)).Value End With With ThisWorkbook.Sheets("Sh2") vaNam = .Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp)).Value End With For i = LBound(vaMn, 1) To UBound(vaMn, 1) For j = LBound(vaNam, 1) To UBound(vaNam, 1) 'put all the first words in a collection vaSplitMn = Split(vaMn(i, 1), Space(1)) Set colUnique = New Collection For lWord = LBound(vaSplitMn) To UBound(vaSplitMn) colUnique.Add vaSplitMn(lWord), LCase(CStr(vaSplitMn(lWord))) Next lWord 'add all the next words to the collection to remove duplicates vaSplitNam = Split(vaNam(j, 1), Space(1)) For lWord = LBound(vaSplitNam) To UBound(vaSplitNam) On Error Resume Next colUnique.Add vaSplitNam(lWord), LCase(CStr(vaSplitNam(lWord))) On Error GoTo 0 Next lWord 'Write to log lMatches = UBound(vaSplitMn) + UBound(vaSplitNam) + 2 - colUnique.Count lTotal = UBound(vaSplitMn) + 1 If lMatches &gt;= lTotal / 2 Then sLog = sLog &amp; "(#" &amp; i &amp; " Sh1) (#" &amp; j &amp; " Sh2): |" &amp; vaMn(i, 1) &amp; "| - |" &amp; vaNam(j, 1) &amp; "| = " sLog = sLog &amp; lMatches &amp; "/" &amp; lTotal &amp; " matches." &amp; vbNewLine End If Next j Next i 'post total log all at once Open ThisWorkbook.Path &amp; Application.PathSeparator &amp; "CompareLog2.txt" For Output As #1 Print #1, sLog Close #1 Debug.Print Timer - sgStart End Sub </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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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