Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default 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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default 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.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 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.

  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Find" a wildcard as a place marker and "replace" with original va Eric Excel Discussion (Misc queries) 1 January 27th 09 06:00 PM
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
change "true" and "false" to "availble" and "out of stock" inthestands Excel Worksheet Functions 2 July 19th 07 07:05 PM
HELP on "left","right","find","len","substitute" functions serene83 Excel Discussion (Misc queries) 5 June 27th 06 02:23 AM
Count occurences of "1"/"0" (or"TRUE"/"FALSE") in a row w. conditions in the next BCB New Users to Excel 7 May 13th 06 10:02 PM


All times are GMT +1. The time now is 10:12 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"