I am trying to speed up the following function using the .find method rather than the original DO loop.
I can't figure out why the str is not found by the .find method
FYI, I have it set up to run both methods of finding the row number to determine if the new method always returns the same as the original method before I delete the original DO loop.
Public Function GetRowByID(str As String) As Long
Dim row As Integer
Dim newrow As Integer
Dim r As Range
Dim r2 As Range
Dim rng As Range
row = 1
' original code
' returns the row number for found string or
' row number of first row where string =""
With Sheets(snOBJECTDATA)
Set r = .Range(OBJECTSTORE_RANGENAME)
Do Until r(row, COLID) = str Or r(row, COLID) = ""
If str = r(row, COLID) Then Exit Do
row = row + 1
Loop
End With
' new code to find the row number faster
newrow = 0
Set rng = Worksheets(snOBJECTDATA).Range(OBJECTSTORE_RANGENAME).Columns(COLID)
Set r2 = rng.Find(What:=str, Lookin:=xlValues, LookAt:=xlWhole)
If Not r2 Is Nothing Then newrow = r2.row
'check if new method result matches original method
If newrow <> row Then
MsgBox ("newrow:" & newrow & " row:" & row)
End If
GetRowByID = row
end function
Thanks in advance.
Bookmarks