Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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.
Written by Allen Wyatt (last updated September 16, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12639) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Capitalizing Just a Surname.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
As you use Excel to collect data over time, sometimes winnowing out the latest data can present a challenge. Here are a ...
Discover MoreExcel makes it easy to delete rows in a worksheet, but it can be more difficult to figure how to delete rows if you only ...
Discover MoreIf you copy a cell that contains a reference to external data, do you get an error? It could be due to the complexity of ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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+.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments