Create a variable with just the first name from cell with full name

AlanCT

New Member
Joined
Jul 11, 2015
Messages
22
I have a spreadsheet which, simplified, is in this format:

EUName >>>>>Product>>>>>>>>>>email
John Smith>>>>Computer>>>>john.smith@abc.com
Sara Jones>>>>Monitor>>>>> sara.jones@abc.com

<end spreadsheet="">
And I have some code (simplified version listed below), that when I put the cursor on a given row, generates an email asking if the EUName would like the Product installed.


If the cursor is on the first row of data, the code:
Set lbEUName = Cells.Find(What:="EUName", SearchOrder:=xlByRows, LookAt:=xlWhole)
Set EUName = Cells(Selection.Row, lbEUName.Column)

creates a variable EUName which has a value of "John Smith"

My question is, what is another line(s) of code that would create a variable EUFirstName which would have a value of "John"?


Thanks,
Alan

<current code,="" simplified<start="" macro="">

<current code,="" simplified="">

Sub CreateEmail()


Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


Set lbEUName = Cells.Find(What:="EUName", SearchOrder:=xlByRows, LookAt:=xlWhole)

Set lbProduct = Cells.Find(What:="Product", SearchOrder:=xlByRows, LookAt:=xlWhole)
Set lbEUEmail = Cells.Find(What:="email", SearchOrder:=xlByRows, LookAt:=xlWhole)


Set EUName = Cells(Selection.Row, lbEUName.Column)
Set Product = Cells(Selection.Row, lbProduct.Column)
Set EUEmail = Cells(Selection.Row, lbEUEmail.Column)

strbody = "Hi " & EUName & "," & vbNewLine & vbNewLine & _
"Can we install the " & Product & "?" & vbNewLine

On Error Resume Next

With OutMail
.To = EUEmail.Value
.Subject = "Install of " & Product
.Body = strbody
.Display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub
</current></current></end>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Add this line after setting the value of EUName:
Code:
Set EUFirstName = Split(EUName, " ")(0)
 
Last edited:
Upvote 0
another way ...

Declare the variablle as a string Dim EUFirstName As String
BELOW Set EUName = Cells(Selection.Row, lbEUName.Column)

INSERT
Code:
EUFirstName = Left(EUName, InStr(EUName, " ") - 1)
 
Last edited:
Upvote 0
another way ...

Declare the variablle as a string Dim EUFirstName As String
BELOW Set EUName = Cells(Selection.Row, lbEUName.Column)

INSERT
Code:
EUFirstName = Left(EUName, InStr(EUName, " ") - 1)
This works fine, except if there is only a first name in the field with no spaces after it, when you will get an error. My suggestion will cater for that ok.
 
Upvote 0
@jmacleary - well spotted :)

How about....
Code:
EUFirstName = Left(EUName, InStr(EUName & " ", " ") - 1)
 
Upvote 0
@jmacleary - a little mod to your code to prevent a mismatch error ... ;)
Code:
EUFirstName = Split(EUName, " ")(0)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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