View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
keepITcool keepITcool is offline
external usenet poster
 
Posts: 2,253
Default searching data thru 20000 records


Nigel,
did you test this with 20.000 records?
I doubt it.. this is SLOW code. It will loop 20000 * 20000 times:
.... in my book that is 400'000'000 loops.


Much better to create indexes first and then compare the indexes.
example see http://groups.google.com
search: excel compare two sets of data author:keepitcool
(post Aug 9, 2005)


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Nigel wrote in

Hi, try this code, it finds the first match on sheet 2 and copies
columns A & B to sheet 1
change references to suit your needs.

'-----------------------------------------------------------------
Sub Update()
Application.ScreenUpdating = False

Dim wsNew As Worksheet, wsOld As Worksheet
Dim xLastRowNew As Long, xLastRowOld As Long
Dim xRN As Long, xRO As Long, XUpdates As Long

Set wsNew = Sheets(1)
Set wsOld = Sheets(2)

' use column 3 ("C") to get last row of data on each sheet
xLastRowNew = wsNew.Cells(Rows.Count, 3).End(xlUp).Row
xLastRowOld = wsOld.Cells(Rows.Count, 3).End(xlUp).Row

' scan new data - assumes first data row is row 1
XUpdates = 0
With wsNew
For xRN = 1 To xLastRowNew
For xRO = 1 To xLastRowOld
' check for match
If .Cells(xRN, 3) = wsOld.Cells(xRO, 3) And _
.Cells(xRN, 4) = wsOld.Cells(xRO, 4) And _
.Cells(xRN, 5) = wsOld.Cells(xRO, 5) Then
' copy values
.Cells(xRN, 1) = wsOld.Cells(xRO, 1)
.Cells(xRN, 2) = wsOld.Cells(xRO, 2)
XUpdates = XUpdates + 1
Exit For
End If
Next xRO
Next xRN
End With
Application.ScreenUpdating = True

MsgBox "Completed - Updated: " & CStr(XUpdates)

End Sub