What's wrong with my UDF?

Ardmore7

New Member
Joined
Mar 11, 2009
Messages
7
My user-defined formula is supposed to find the address of a cell that contains "TextToLookFor" in worksheet "Sheet"? Can you telll me what is wrong with it?


Function FindIt(TextToLookFor, Sheet) As String

Dim myCell As Range

myCell = Cells.Find(What:=TextToLookFor, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, MatchCase:=False, SearchFormat:=False).Activate
FindIt = myCell.Address(False, False)

End Function
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Syntactically your use of Find is incorrect (you would need to use a Set statement), also you can't use a UDF to actually activate any other part of the sheet in which it resides. Additionally, you haven't specified that sheet Sheet is to be searched. Try the following:

Code:
Function FindIt(ByVal TextToLookFor As String, ByVal strSheet As String) As String
    
    Dim myCell As Range

With Sheets(strSheet)
    
    Set myCell = .Cells.Find(What:=TextToLookFor, LookIn:=xlFormulas, _
        LookAt:=xlPart)

End With

If Not myCell Is Nothing then
       FindIt = myCell.Address(False, False)
Else
  FindIt = "Not Found!"
End If
    
End Function
You will also need to be using at least xl2002 for this to work I believe.

EDIT: use in your sheet like this:

=FINDIT("SomeText","YourSheetNameHere")

Richard
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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
Back
Top