Solved

Reverse names in Excel

Posted on 2014-11-12
2
152 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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