
Tips.Net > ExcelTips Home > Macros > Comparing Strings
Summary: Comparing strings is something that is routinely done in a macro, particularly when getting input from a user. To successfully compare what is entered with a value, you need to do some conversions on the data entered. This tip explains the logic behind such an approach, along with an example of how to do it. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
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(UserIn$) = "yes" then DoIt = True
The trick is make sure your test string is either all upper or all lower case, and then convert the user's input to that same case.
Tip #2279 applies to Microsoft Excel versions: 97 2000 2002 2003
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.
Check out ExcelTips: The Macros today!
Want to make Excel do even more? The way is easy when you know how to use macros. This great e-book makes it easy. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site