ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Replace "FIRSTNAME LASTNAME" with "LAST" (https://www.excelbanter.com/excel-programming/408116-replace-firstname-lastname-last.html)

chrishutson123

Replace "FIRSTNAME LASTNAME" with "LAST"
 
Hello:

I have a cell with the string "FIRSTNAME LASTNAME". I would like to replace
it with "LAST". I have some special cases:

"MERRILL C CURRIER" should yield "CURR"
"JON SUN" should yield "SUN "
"JON C SUN" should yield "SUN "
"TRAVIS SMITH" should yield "SMIT"

Can anyone help? I am very appreciative. You should know that I am new to
VBA in Excel. Thanks.

Chris.

Gary''s Student

Replace "FIRSTNAME LASTNAME" with "LAST"
 
Without VBA:

In B1, enter:
=SEARCH("|",SUBSTITUTE(A1," ","|",(LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))+1


In C1, enter:
=MID(A1,B1,4)

With VBA:

Sub lastname()
s = Split(ActiveCell.Value, " ")
u = UBound(s)
MsgBox (Left(s(u), 4))
End Sub

--
Gary''s Student - gsnu200775


"chrishutson123" wrote:

Hello:

I have a cell with the string "FIRSTNAME LASTNAME". I would like to replace
it with "LAST". I have some special cases:

"MERRILL C CURRIER" should yield "CURR"
"JON SUN" should yield "SUN "
"JON C SUN" should yield "SUN "
"TRAVIS SMITH" should yield "SMIT"

Can anyone help? I am very appreciative. You should know that I am new to
VBA in Excel. Thanks.

Chris.


Don Guillett

Replace "FIRSTNAME LASTNAME" with "LAST"
 
try
Sub truncatenames()
For Each c In Range("e2:e25")
x = InStrRev(c, " ") + 1
'MsgBox x
If (Len(c) - x) < 4 Then
c.Value = Right(c, Len(c) - x + 1)
Else
c.Value = Mid(c, x, 4)
End If
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"chrishutson123" wrote in message
...
Hello:

I have a cell with the string "FIRSTNAME LASTNAME". I would like to
replace
it with "LAST". I have some special cases:

"MERRILL C CURRIER" should yield "CURR"
"JON SUN" should yield "SUN "
"JON C SUN" should yield "SUN "
"TRAVIS SMITH" should yield "SMIT"

Can anyone help? I am very appreciative. You should know that I am new
to
VBA in Excel. Thanks.

Chris.



chrishutson123

Replace "FIRSTNAME LASTNAME" with "LAST"
 
Thank you so much!!! That worked like a champ. I have another question:

I have a cell with account numbers formatted like "1234-2" or "12345-2" or
"123456-2". The main part of the account number could be as few as 3
characters and as many as 6 characters followed by a "-2". I need to convert
this field to a 40 character string with leading zeroes. Can you help?

"Gary''s Student" wrote:

Without VBA:

In B1, enter:
=SEARCH("|",SUBSTITUTE(A1," ","|",(LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))+1


In C1, enter:
=MID(A1,B1,4)

With VBA:

Sub lastname()
s = Split(ActiveCell.Value, " ")
u = UBound(s)
MsgBox (Left(s(u), 4))
End Sub

--
Gary''s Student - gsnu200775


"chrishutson123" wrote:

Hello:

I have a cell with the string "FIRSTNAME LASTNAME". I would like to replace
it with "LAST". I have some special cases:

"MERRILL C CURRIER" should yield "CURR"
"JON SUN" should yield "SUN "
"JON C SUN" should yield "SUN "
"TRAVIS SMITH" should yield "SMIT"

Can anyone help? I am very appreciative. You should know that I am new to
VBA in Excel. Thanks.

Chris.


Don Guillett

Replace "FIRSTNAME LASTNAME" with "LAST"
 
Custom format
000000-2

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"chrishutson123" wrote in message
...
Thank you so much!!! That worked like a champ. I have another question:

I have a cell with account numbers formatted like "1234-2" or "12345-2" or
"123456-2". The main part of the account number could be as few as 3
characters and as many as 6 characters followed by a "-2". I need to
convert
this field to a 40 character string with leading zeroes. Can you help?

"Gary''s Student" wrote:

Without VBA:

In B1, enter:
=SEARCH("|",SUBSTITUTE(A1," ","|",(LEN(A1)-LEN(SUBSTITUTE(A1,"
","")))))+1


In C1, enter:
=MID(A1,B1,4)

With VBA:

Sub lastname()
s = Split(ActiveCell.Value, " ")
u = UBound(s)
MsgBox (Left(s(u), 4))
End Sub

--
Gary''s Student - gsnu200775


"chrishutson123" wrote:

Hello:

I have a cell with the string "FIRSTNAME LASTNAME". I would like to
replace
it with "LAST". I have some special cases:

"MERRILL C CURRIER" should yield "CURR"
"JON SUN" should yield "SUN "
"JON C SUN" should yield "SUN "
"TRAVIS SMITH" should yield "SMIT"

Can anyone help? I am very appreciative. You should know that I am
new to
VBA in Excel. Thanks.

Chris.



Rick Rothstein \(MVP - VB\)[_1530_]

Replace "FIRSTNAME LASTNAME" with "LAST"
 
With VBA:

Sub lastname()
s = Split(ActiveCell.Value, " ")
u = UBound(s)
MsgBox (Left(s(u), 4))
End Sub


Another way...

MsgBox Left(Mid(ActiveCell.Value, InStrRev(ActiveCell.Value, " ") + 1), 4)

Rick


All times are GMT +1. The time now is 10:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com