A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Looks like the problem is that SEARCH is an Excel worksheet function, not a native VBA function. When you write VBA, you shouldn't call Search() directly, so VBA reports “Sub or Function not defined.” You can use the VBA equivalent, InStr, instead.
The VBA function would be:
Function FirstName(Text1) FirstName = Right(Text1, Len(Text1) - InStr(Text1, " ")) End Function
For example, if A1 contains Smith John, the Excel formula =FirstName(A1) will return John.
You can also explicitly call the Excel SEARCH function from VBA:
Function FirstName(Text1) FirstName = Right(Text1, Len(Text1) - Application.WorksheetFunction.Search(" ", Text1)) End Function
Consider using the InStr version because InStr is the native VBA function for finding the position of a character or string.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin