Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, 2024, 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: Comparing Strings.
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.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11704) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Comparing Strings.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel Data Analysis and Business Modeling today!
One of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...
Discover MoreIf you manually recalculate your workbooks, you are probably doing so because of the time it takes to do the ...
Discover MoreTracking down memory errors in a macro can be frustrating. The error message is inherently vague and correcting any ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2026-07-06 09:47:29
J. Woolley
When comparing strings as described in the Tip, consider trimming possible leading and/or trailing space characters:
If LCase(Trim(sUserIn)) = "yes" Then bDoIt = True
Notice VBA's Trim function removes any leading and trailing spaces but does not remove subsequent extraneous interior spaces. If that is necessary, use the following statement:
If LCase(WorksheetFunction.Trim(sUserIn)) = "yes" Then bDoIt = True
This invokes Excel's TRIM function which leaves only a single space between words after stripping space characters from the text's beginning and end.
In either case, the VBA statements can be simplified as follows:
bDoIt = (LCase(...) = "yes")
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 © 2026 Sharon Parq Associates, Inc.
Comments