I'm getting cross-eyed, what mistake am I making?
Several.
- Parameter
Rangeis declared as aString, but clearly used as if it were aRangeobject. It should be declaredAs Range. - The name
Rangeis hidingGlobal.Range, which may or may not be a problem (it isn't, in this case). Hiding/shadowing existing declarations in wider scopes is generally a bad idea. - The function is implicitly
Public. Better if explicitly so. - Parameters are implicitly passed
ByRef, but there's no justification for it; they should be passedByVal. - Parameter
searchCellis an implicitVariant, but it's used as if it were aRangeobject; declare itAs Range. - Function returns an implicit
Variant, but really returns aString. Signature should specifyAs Stringfor the return type. - Local variable
cellisn't declared, which means it's an on-the-fly implicitVariant. Declare it explicitly,As Range. - Code that compiles with undeclared variables doesn't have
Option Explicitspecified, which means VBA will happily compile and run any typo. Avoid stupid embarrassing problems, specifyOption Explicitat the top of every single module, and declare every single variable. - "No Match" return value is needlessly re-assigned on every iteration.
- Function name is
camelCase, but public members in every single VBA type library are consistentlyPascalCase. - Indentation isn't consistent.
Rubberduck (an open-source VBE add-in project I manage) would have picked up most of these points with its static code analysis.
Option Explicit Public Function BetterSearch(ByVal searchCell As Range, ByVal source As Range) As String Dim cell As Range For Each cell In source If cell.Value = searchCell.Value Then BetterSearch = "Match" Exit ForFunction End If Next BetterSearch = "No match" End Function IMO the function would be much more useful if it returned a Boolean rather than a "magic string". True when found, False when not found.