Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Capitalizing Just a Surname.

Capitalizing Just a Surname

Written by Allen Wyatt (last updated September 16, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365


4

Cheryl is using a worksheet that has, in column A, client names in the format "Smith, Jane." She would like to capitalize only the surname, as in "SMITH, Jane", leaving the rest of the name unchanged.

If there is one and only one comma that separates the surname from the first name, you can create a formula to do the conversion. Assuming the name is in A1, the formula would be:

=UPPER(LEFT(A1,FIND(",",A1)-1))&MID(A1,FIND(",",A1),LEN(A1))

If you are using Microsoft 365, you can use a more robust formula that checks to see if the name being evaluated includes a comma:

=LET(x,FIND(",",A1),IF(IFERROR(x,0)>0,UPPER(LEFT(A1,x-1))&MID(A1,x,LEN(A1)),A1))

If it does not, then the original name, unchanged, is returned by the formula.

If you prefer to not use a formula (which may mess up the look of your worksheet), you could also use a macro to convert the names, in place. Consider the following:

Sub CapitalizeSurnames()
    Dim rCell As Range
    Dim iComma As Integer
    For Each rCell In Selection
        iComma = InStr(rCell, ",")
        If iComma > 0 Then
            rCell = UCase(Left(rCell, iComma - 1)) & _
              Mid(rCell, iComma)
        End If
    Next
    Set rCell = Nothing
End Sub

Simply select the cells that you want to convert (such as those in column A) and then run the macro. It makes the conversion to the names in the cells.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12639) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Capitalizing Just a Surname.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Checking Up On Numbers

When do you use digits in your prose and when do you spell out the numbers? Why not let Word help you make the decision? ...

Discover More

Don't Update Links to Other Programs

If you have links in your workbook to data in other workbooks, you may want to control whether Excel updates those links ...

Discover More

Getting Rid of Cells Containing Only Spaces

If you have a worksheet that contains a bunch of cells that contain nothing but spaces, you may be looking for a way to ...

Discover More

Program Successfully in Excel! This guide will provide you with all the information you need to automate any task in Excel and save time and effort. Learn how to extend Excel's functionality with VBA to create solutions not possible with the standard features. Includes latest information for Excel 2024 and Microsoft 365. Check out Mastering Excel VBA Programming today!

More ExcelTips (ribbon)

Pasting Multiple Paragraphs Into a Single Cell

Copying information from one program (such as Word) to another (such as Excel) is a common occurrence. If you want to ...

Discover More

Not Enough Resources to Delete Rows and Columns

Few things are as frustrating as trying to delete rows or columns and having Excel tell you that you can't perform the ...

Discover More

Displaying a Hidden First Row

If you hide the first rows of a worksheet, you may have a hard time getting those rows visible again. Here's a simple way ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five minus 1?

2023-09-22 11:13:01

Willy Vanhaelen

@J. Woolley
Oops! I know that with the Worksheet_Change event Sub you often must disable events but as I made this UDF in a hurry, I forgot. :-( This is not an excuse because VBA doesn’t “forget” anything and I should not post a macro without thoroughly testing it.

For your remarks 2 and 3. You are of course right but one is not supposed to enter a formula in a column that must have only names in it. The purpose of the UDF is to enable you to enter new names in a list all lower case, one by one, transforming the surname automatically in upper case.


2023-09-20 15:39:13

J. Woolley

@Willy Vanhaelen
I posted a similar event procedure on the same day here: https://excelribbon.tips.net/T012550_Using_an_Input_Mask.html
Therefore, I noticed some issues with your procedure:
1. You need to disable events before changing Target; otherwise, the event procedure will repeat many times (46) for that Target.
2. If you enter a formula in column A that results in text with a comma, that formula will be replaced by a constant equal to the modified result.
3. If you copy N rows of names and paste them into column A, when the first name includes a comma it gets modified and duplicated in N rows of column A; otherwise, the N rows are pasted without change.
Here is an improved version of your procedure:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim NameCells As Range, cell As Range, valu As String, iComma As Integer
    Set NameCells = Range("A:A") 'adjust as requuired
    For Each cell In Target
        If Not (Application.Intersect(cell, NameCells) Is Nothing _
            Or cell.HasFormula) Then
            valu = cell.Value
            iComma = InStr(valu, ",")
            If iComma > 0 Then
                valu = UCase(Left(valu, iComma - 1)) & Mid(valu, iComma)
                Application.EnableEvents = False
                    cell.Value = valu 'low risk of error
                Application.EnableEvents = True
            End If
        End If
    Next cell
End Sub


2023-09-17 13:56:54

Willy Vanhaelen

Here is a macro that does the capitalization at the moment you enter the name (for column A):

Private Sub Worksheet_Change(ByVal Target As Range)
If Left(Target.Address, 3) <> "$A$" Then Exit Sub
Dim iComma As Integer
iComma = InStr(Target(1), ",")
If iComma = 0 Then Exit Sub
Target = UCase(Left(Target(1), iComma - 1)) & Mid(Target(1), iComma)
End Sub

Change "($A$)" according to the column letter(s) you are using.


2023-09-16 10:09:49

J. Woolley

I believe the LET function is in Excel 2021 and later. Since Excel 365 is always later, I like to reference Excel 2021+ as shorthand when discussing such functions. And I encourage anyone how is serious about Excel to upgrade to Excel 2021+.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.