You are here:

Excel/reorder and truncate text in cell

Advertisement


Question
QUESTION: cell a1 has text as such: "My name is P.Emrich"
a2 is "My friend's name is A.Heritage"

I want to be able to replace the text in the cells with just "Emrich,P"
and "Heritage,A"

Can you suggest a macro to just get the last name followed by a comma and the initial?

Pete

ANSWER: Providing that the rule is always that the name is at the end, then you COULD do it with a formula

=SUBSTITUTE(MID(A1,SEARCH(".",A1)-1,2)&RIGHT(A1,LEN(A1)-SEARCH(".",A1)),".",",")

by putting this in a blank column, you will get the value you want - you could then copy and paste special, values over the existing data.  This has the advantage of displaying the data you are going to use before destroying the original - which a macro would do.

A macro using the same method would be

For Each cell In Range("A1:a2")
cell.Value = WorksheetFunction.Substitute(Mid(cell.Value, WorksheetFunction.Search(".", cell.Value) - 1, 2) & Right(cell.Value, Len(cell.Value) - WorksheetFunction.Search(".", cell.Value)), ".", ",")
Next


replace my a1:a2 with the actual range you are testing.

---------- FOLLOW-UP ----------

QUESTION: Thanks Aidan, but the formula gives me P,Emrich not Emrich,P

Did not test the macro.

Pete

Answer
Sorry, blame it on the long day here in the UK coupled with a 4 month old - try


=RIGHT(A1,LEN(A1)-SEARCH(".",A1))&","&MID(A1,SEARCH(".",A1)-1,1)

for the formula method - amend the macro in a similar way (let me know if this isn't clear though!)
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Aidan Heritage

Expertise

I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!

Experience

My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

Education/Credentials
I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!

©2016 About.com. All rights reserved.