Sam has a large number of addresses in a worksheet. In those addresses he needs to make sure that all compass directions (NE, SE, NW, and SW) are all uppercase. It would be very helpful if Sam could figure out how to change any of these lowercase (or mixed case) directions that appear only at the end of a cell with their uppercase counterparts. He can't just search for a space followed by "ne", as that would change Newton to NEwton, so he wonders how he can make sure that the replacement occurs only when the letters appear at the end of a cell.
There is no way to accomplish this task using the Find and Replace tools in Excel. That means that you need to use a formula or a macro to do the task. Formulas can be used to make sure that the last two characters of a cell are uppercase:
=LEFT(A1,LEN(A1)-2) & UPPER(RIGHT(A1,2))
The problem with such a formula, however, is that it is non-discriminating. As long as any cell it is used on has a compass direction as its last two characters, there is no problem. But if some cells don't have the compass direction, then you run into problems real fast. In that case you need to actually have the formula check the last characters:
=IF(RIGHT(A1,3)=" ne", LEFT(A1,LEN(A1)-2) & "NE", IF(RIGHT(A1,3)=" se", LEFT(A1,LEN(A1)-2) & "SE", IF(RIGHT(A1,3)=" nw", LEFT(A1,LEN(A1)-2) & "NW", IF(RIGHT(A1,3)=" sw", LEFT(A1,LEN(A1)-2) & "SW", A1))))
This formula checks the last three characters to see if there is a space followed by either ne, se, nw, or sw. If this is the case, then those last two characters are made uppercase. The formula can be shortened if you approach it differently:
=IF(OR(RIGHT(A1,3)=" ne", RIGHT(A1,3)=" se", RIGHT(A1,3)=" nw", RIGHT(A1,3)=" sw"), LEFT(A1,LEN(A1)-2) & UPPER(RIGHT(A1,2)), A1)
You can shorten it even more by using an array of compass directions in the formula:
=IF(OR(RIGHT(A1,3)={" ne"," se"," sw"," nw"}), LEFT(A1,LEN(A1)-2) & UPPER(RIGHT(A1,2)), A1)
If you prefer to not use a formula, you can easily create a macro that will do the checking and conversion for you:
Sub CapDirections() For Each RCell In Selection CText = UCase(Right(RCell.Value, 3)) If CText = " NE" Or CText = " SE" _ Or CText = " SW" Or CText = " NW" Then RCell.Value = Left(RCell.Value, _ Len(RCell.Value) - 3) + CText End If Next End Sub
To use the macro, just select the cells containing the addresses, and then run it. It checks to see if one of the four compass points are at the end of the cell value, and if it is then it makes sure that the compass direction is uppercase.
You should note that these solutions are based upon there only being four possible compass directions in your addresses. If your addresses have more wide-ranging compass directions (like N or SSE) then you will definitely want to use a macro-based solution because the checking quickly becomes very complex for a formula.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9746) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Replacing Characters at the End of a Cell.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Finding and replace dates contained within other text in a cell can be a bit tricky. This tip looks at some approaches to ...
Discover MoreThe Find and Replace capabilities of Excel allow you to easily locate all the cells in a worksheet that contain specific ...
Discover MoreThe find and replace used in Excel is less powerful than its counterpart in Word, so it is not able to do some of the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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 © 2022 Sharon Parq Associates, Inc.
Comments