Solved

Reverse names in Excel

Posted on 2014-11-12
2
159 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 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 Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

718 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