?
Solved

Reverse names in Excel

Posted on 2014-11-12
2
Medium Priority
?
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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