Find Method loop is slowing down

arjking

New Member
Joined
May 23, 2008
Messages
11
Thanks to this forum I have recently learned to use the Find Method.

My code is designed to detect a row in one table that does not exist in another table. I loop through every cell in the first table and test a Find statement searching the other table. This loops through about 2000 times. I noticed via the immediate window that each find takes a little longer than the last so that by the time I get to the upper 1000's it is crawling. I'm curious as to why this is so and what I can do to fix it.

BLANKINCOLUMN and LASTINCOLUMN are functions that find the the last cell/blank in a column.

Code:
Private Sub ReportNewRowsA()
  Dim rngVLImport1, rngVLImport2, rngVLCurrent1, rngVLCurrent2, cell1, cell2 As Range
  With Workbooks(wbFutureName).Sheets(wbImportName)
    Set rngVLImport1 = .Range("A3:A" & LASTINCOLUMN(.Range("A3"))) 'This is the table with the new rows
    Set rngVLImport2 = .Range("DA3:DA" & LASTINCOLUMN(.Range("DA3"))) 'Not used
  End With
 
  With Workbooks(wbFutureName).Sheets(wbCurrentName)
    Set rngVLCurrent1 = .Range("A3:A" & LASTINCOLUMN(.Range("A3"))) 'This is the table being searched
    Set rngVLCurrent2 = .Range("DA3:DA" & LASTINCOLUMN(.Range("DA3"))) 'Not used
  End With
 
  With Workbooks(wbFutureName)
    For Each cell1 In rngVLImport1.Cells
      If rngVLCurrent1.Find(What:=cell1.Value, _
                            After:=.Sheets(wbCurrentName).Range("A3"), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext) Is Nothing Then
        .Sheets(wbImportName).Range("A" & cell1.row & ":AH" & cell1.row).Copy _
        Destination:=.Sheets("New Rows").Range("A" & BLANKINCOLUMN(.Sheets("New Rows").Range("A1")))
        'Debug.Print BLANKINCOLUMN(.Sheets("New Rows").Columns("A"))
      End If
      Debug.Print cell1.Address
    Next cell1
  End With
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,191,169
Messages
5,985,059
Members
439,937
Latest member
MAlhash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top