UDF Function to extract first word in a string

freonthewhite

New Member
Joined
Mar 21, 2025
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm having trouble doing the VBA to create my own function. I have the excel formula to do it, but I just want to be able to have it be a function so that I can do something like =FirstWord(A2)
The excel formula that does this is:
=IF(ISERR(FIND(" ",A2)),A2, LEFT(A2, FIND(" ",A2)-1))

So I just want the function to feed the cell reference so that instead of using this formula I can just do =FirstWord(A2)

I tried the below, but it looks like I can't just do what I did.

Any help is appreciated, thank you

VBA Code:
Option Explicit

Function FirstWord(input_data)

    input_data = IF(ISERR(FIND(" ",input_data)),input_data, LEFT(input_data, FIND(" ",input_data)-1))
       
    FirstWord = input_data(word)
    
End Function
 
Code:
Function FirstWord(ByVal input_data As String) As String
    input_data = Trim(input_data) & " "      ' So that FirstWord("London") --> "London"
    FirstWord = Left(input_data, InStr(1, input_data, " ") - 1)
End Function
 
Upvote 0
Solution
Why not just use the textbefore function
Fluff.xlsm
AB
1
2LindleyLindley
3Penistone EastPenistone
4LongholmeLongholme
5Hyde WernethHyde
Data
Cell Formulas
RangeFormula
B2:B5B2=TEXTBEFORE(A2," ",,,1)
 
Upvote 0
Code:
Function FirstWord(ByVal input_data As String) As String
    input_data = Trim(input_data) & " "      ' So that FirstWord("London") --> "London"
    FirstWord = Left(input_data, InStr(1, input_data, " ") - 1)
End Function
Thank you, this looks great, but for some reason I can't use it by just doing =frankshort(A1)
If I look in the list for the user defined functions then it shows up like in the picture. It will work if I pick it this way, but it pops up a window asking for the input.
Any idea why that is?
Function problem.JPG
 
Upvote 0
If you want the udf in your personal macro workbook you need to type
Excel Formula:
=personal.xlsb!frankshort(A1)
 
Upvote 0
It will be faster & more efficient than a UDF.
For you and me yes, but the people that I'm going to be setting this up for are people who struggle with copy/paste, that's why I'm trying to make it as easy as possible.
 
Upvote 0
If you want the udf in your personal macro workbook you need to type
Excel Formula:
=personal.xlsb!frankshort(A1)
Dang, so it can't be just
Excel Formula:
=frankshort(A1)
if it's in the persconal workbook?
 
Upvote 0
If you use the "Insert Function" window, this happens, not only with frankshort but with every Excel function, e.g. Find. Click where you want to enter the formula, e.g. B1 --> type =f. With every Excel function, the same, you have to remember what the function is called, e.g. SUBS***. And then you type =s. So when you type =f, a list opens, where you see the function frankshort. Select this function ...
 
Upvote 0

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