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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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