VBA First Name

BrianM

Well-known Member
Joined
Jan 15, 2003
Messages
768
Office Version
  1. 2016
Platform
  1. Windows
Hello all,
Using VBA, what is a simple line of code to obtain the first name from A1 (John Doe) so that I can set it as a variable?

My brain says
Dim FName as string
FName =LEFT(A2,SEARCH(“ ”,A2)-1) but I know that won't work. I don't want to use a formula in a helper cell to only then set the variable.

Thanks
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try

FName = Left(Range("A1"), InStr(Range("A1") & " ", " ") - 1)

or

FName = Split(Range("A1"))(0)
 
Last edited:
Upvote 0
There is no such thing as "perfect" when it comes to names. Eric's code (pretty much any code) will fail to handle multiple first names. For example, back in my working days, I had a co-worker whose given (legal) first name was Mary Ann (with the space between them).
 
Upvote 0
There is no such thing as "perfect" when it comes to names. Eric's code (pretty much any code) will fail to handle multiple first names. For example, back in my working days, I had a co-worker whose given (legal) first name was Mary Ann (with the space between them).

Agreed... With that being said, this is what I'm using
FNameGR = Split(Range("B27"))(0)
FNameMAR = Split(Range("B28"))(0)
FNameFTR = Split(Range("B29"))(0)

However, there are cases where any one of these cells could be empty, how would I handle those cases? As is, I get Subscript out of range when I run into an empty cell.

Thanks
 
Upvote 0
What do you want in case of an empty cell? The first formula I posted won't give you the Subscript out of range error, it will just return a null string. If you want something else, then let us know and we can figure something out.
 
Upvote 0
What do you want in case of an empty cell? The first formula I posted won't give you the Subscript out of range error, it will just return a null string. If you want something else, then let us know and we can figure something out.

Something like the following:

If IsEmpty(Range("B27").Value) = True Then
MsgBox "Government Rep field is empty."
Else

FNameGR = Split(Range("B27"))(0)

End If

If IsEmpty(Range("B28").Value) = True Then
MsgBox "Mission Assurance Representative field is empty."
Else

FNameMAR = Split(Range("B28"))(0)

End If

If IsEmpty(Range("B29").Value) = True Then
MsgBox "Functional / Technical Representative."

Else

FNameFTR = Split(Range("B29"))(0)

End If
Something like that...
 
Last edited:
Upvote 0
That's seems fine, although you might want to try:

Code:
If Range("B27").Value = "" Then
    MsgBox [COLOR=#333333]"Government Rep field is empty."
    Exit Sub
Else
    [/COLOR][COLOR=#333333]FNameGR = Split(Range("B27"))(0)[/COLOR]
[COLOR=#333333]End If

etc . . . [/COLOR]
 
Upvote 0
That's seems fine, although you might want to try:

Code:
If Range("B27").Value = "" Then
    MsgBox [COLOR=#333333]"Government Rep field is empty."
    Exit Sub
Else
    [/COLOR][COLOR=#333333]FNameGR = Split(Range("B27"))(0)[/COLOR]
[COLOR=#333333]End If

etc . . . [/COLOR]

Perfect :D

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,973
Members
449,200
Latest member
Jamil ahmed

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