Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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: Comparing Strings.
by Allen Wyatt
(last updated October 24, 2015)
It is not uncommon to compare strings in a macro. For instance, you may need to compare what a user typed with some pre-determined value. If you do this directly, you must take into consideration that the user may not have typed his (or her) string in the same way as you expected. Particularly vexing is the fact that the user may have mixed upper and lower case in their response.
The quickest and easiest way around this is to use either the UCase() or LCase() function on their input before you do the comparison. For instance, let's assume you prompt the user for the word "yes" to verify they want an action done. The following code will check the input, regardless of how the user typed it.
If LCase(sUserIn) = "yes" then bDoIt = True
The trick is to make sure your test string is either all upper or all lower case, and then convert the user's input to that same case.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11704) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Comparing Strings.
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!
Your company may be regulated by requirements that it document any changes to the macros in an Excel worksheet. Your options ...Discover More
Need to know how to generate a full month name based on a date? It's easy to do, as discussed in this tip.Discover More
Named ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do you ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.