Solved

Reverse names in Excel

Posted on 2014-11-12
2
145 Views
Last Modified: 2014-11-12
I currently have a function using VBA code where a user can hit a short cut key (Ctrl+R), enter the column they wish to edit, and once they choose the column all names in the column are automatically name reversed. So, for instance, if before the column read SMITH, JOHN, after running this function it reads John Smith. It removes the comma, reverses the name makes it proper (Upper and Lower).

All was well except now it appears we need the same feature except all caps. So if it starts as proper or lower case it needs to end up as upper case.

I'm very new to VBA and I don't know the code very well yet. I know there is a way to edit the existing code so it changes it to all upper but all my attempts either do nothing or give me an error (Run time error 438 appears to be the most common.)

Here is the code in question.

Sub Reverse_Middle_Names()
 Dim i, LastRow
 selcol = InputBox("Please enter the column letter with names", "Select Column")
 LastRow = Range(selcol & Rows.Count).End(xlUp).Row
 For i = 1 To LastRow
 numWords = Len(Cells(i, selcol)) - Len(Application.Substitute(Cells(i, selcol), " ", ""))
 If numWords = 1 Then
 pos = Val(Application.Find(" ", Cells(i, selcol)))
 Cells(i, selcol).Value = Application.Proper(Mid(Cells(i, selcol), pos + 1, _
 (Len(Cells(i, selcol)) - (pos - 1))) & " " & Left(Cells(i, selcol), pos - 1))
 ElseIf numWords = 2 Then
 pos = Val(Application.Find(" ", Cells(i, selcol)))
 Cells(i, selcol).Value = Application.Proper(Mid(Cells(i, selcol), pos + 1, _
 (Len(Cells(i, selcol)) - (pos - 1))) & " " & Left(Cells(i, selcol), pos - 1))
 pos = Val(Application.Find(" ", Cells(i, selcol)))
 Cells(i, selcol).Value = Application.Proper(Mid(Cells(i, selcol), pos + 1, _
 (Len(Cells(i, selcol)) - (pos - 1))) & " " & Left(Cells(i, selcol), pos - 1))
ActiveSheet.UsedRange.Replace what:=",", replacement:="", Lookat:=xlPart




End If
Next
End Sub
0
Comment
Question by:Tarkisal
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40437837
Try changing the "Application.Proper" to "UCase".
0
 

Author Closing Comment

by:Tarkisal
ID: 40438031
That simple change was all it took! I was trying to put in UCase as "Application.Ucase" not realizing the error. By just putting in "UCase" it worked perfectly.

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now