Code Request for Names

mole999

Well-known Member
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Any one have a small bit of code that would take a name and swap it around

Select the cell, button press reads the cell value and

John Smith to become SMITH John. Not worried if it doesn't handle more than two bits, have a number to work through, the odd one is not too important in the grand scheme.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
How about
Code:
Sub mole999()
   Dim St As String, i As Long
   St = ActiveCell.Value
   i = InStrRev(St, " ")
   ActiveCell.Offset(, 1).Value = Right(St, Len(St) - i) & " " & Left(St, i - 1)
End Sub
 
Upvote 0
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Jan30
[COLOR="Navy"]Dim[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
Sp = Split(Selection, " ")
[COLOR="Navy"]For[/COLOR] n = UBound(Sp) To 0 [COLOR="Navy"]Step[/COLOR] -1
    Txt = Txt & IIf(Txt = "", Sp(n), " " & Sp(n))
[COLOR="Navy"]Next[/COLOR] n
Selection = Txt
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
How about
Code:
Sub mole999()
   Dim St As String, i As Long
   St = ActiveCell.Value
   i = InStrRev(St, " ")
   ActiveCell.Offset(, 1).Value = [B][COLOR="#FF0000"]UCase([/COLOR][/B]Right(St, Len(St) - i)[B][COLOR="#FF0000"])[/COLOR][/B] & " " & Left(St, i - 1)
End Sub
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Jan30
[COLOR="Navy"]Dim[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
Sp = Split(Selection, " ")
[COLOR="Navy"]For[/COLOR] n = UBound(Sp) To 0 [COLOR="Navy"]Step[/COLOR] -1
    Txt = Txt & IIf(Txt = "", [B][COLOR="#FF0000"]UCase([/COLOR][/B]Sp(n)[B][COLOR="#FF0000"])[/COLOR][/B], " " & Sp(n))
[COLOR="Navy"]Next[/COLOR] n
Selection = Txt
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
The red additions to Fluff's and Mick's codes above will upper case the last name when moved into the first position (as your original post indicated you wanted).
 
Last edited:
Upvote 0
The red additions to Fluff's and Mick's codes above will upper case the last name when moved into the first position (as your original post indicated you wanted).

Thanks Rick

I was after
Code:
Sub mole999()
On Error Resume Next
   Dim St As String, i As Long
   St = ActiveCell.Value
   i = InStrRev(St, " ")
   ActiveCell.Offset(, 0).Value = UCase(Right(St, Len(St) - i)) & " " & WorksheetFunction.Proper(Left(St, i - 1))
End Sub
Does what I need
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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