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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,060
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
Add this line after setting the value of EUName:
Code:
Set EUFirstName = Split(EUName, " ")(0)
 
Last edited:
Upvote 0

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
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

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,060
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
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

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
ADVERTISEMENT
@jmacleary - well spotted :)

How about....
Code:
EUFirstName = Left(EUName, InStr(EUName & " ", " ") - 1)
 
Upvote 0

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
ADVERTISEMENT
@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,195,746
Messages
6,011,416
Members
441,613
Latest member
worksux

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
Top