Solved

Reverse names in Excel

Posted on 2014-11-12
2
142 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
Comment Utility
Try changing the "Application.Proper" to "UCase".
0
 

Author Closing Comment

by:Tarkisal
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

10 Experts available now in Live!

Get 1:1 Help Now