The find routine is setting a range value to a variable. This requires proper syntax to work properly without returning an error. Note that I use Option Explicit so I define my variables first.
The proper use of Find routine:
-----------------------------------------------------------------------------------------
'Shows an example on how Find and FindNext work in Excel. Note that in the below example,
' FindNext uses the same range varible for both the solution and the 'seed' value so that
' the next time the same code is executed it would be the second found value being the starting
' point instead of the first found
Dim rFound As Range, iFirstRow As Long, iSecondRow As Long, sMessage As String
Set rFound = Worksheets("SheetName").Range("C:C").Find("Name", lookin:=xlValues) 'Finds first instance only
If Not rFound Is Nothing Then
iFirstRow = rFound.Row
Set rFound = Worksheets("SheetName").Range("C:C").FindNext(rFou nd) 'Finds next starting from last found
If Not rFound Is Nothing Then iSecondRow = rFound.Row 'If no value is found, rFound would be Nothing.
End If
sMessage = "First Found: "
If iFirstRow > 0 Then sMessage = sMessage & iFirstRow Else sMessage = sMessage & "N/A"
sMessage = sMessage & vbCrLf & "Second Found: "
If iSecondRow > 0 Then sMessage = sMessage & iSecondRow Else sMessage = sMessage & "N/A"
MsgBox sMessage
-----------------------------------------------------------------------------------------
Hope this clears things up for you.
|