Today, while answering a reader’s email, I wrote this VBA code,
If Target.Value = "yes" Then
'do something
End If
But I realized that my code would run only the Target cell has “yes” in it. It wont run if the target cell has “YES”, or “Yes” or “YeS”.
This is because by default, all VBA comparisons are binary. That means, “yes” ≠ “Yes”.
One quick work-around for this problem is to use UCASE to convert target.value to Uppercase and then compare, like this,
If UCASE(Target.Value) = "YES" Then
'do something
End If
But this seemed painful, especially, if I had to do similar comparison at multiple places in my code, I had to use UCASE() everywhere.
If only there is an option to tell VBA how to compare?!?
Well, there is an option.
Use Option Compare Text
If you write Option Compare Text at the top of your module, all the VBA comparisons with in that module will use Text comparison instead of Binary comparison. Thus, “yes” will be equal to “YES”.
Do you use Option Compare?
There are 3 settings for Option Compare.
Option Compare Binary: This is the default setting. Compares everything at binary level.Option Compare Text: Used for situations like this.Option Compare Database: Can be used only with MS Access VBA. Uses Database Table settings to determine how to compare.
This is the first time I have used Option Compare Text. But it seems like an elegant way to tell Excel VBA how to compare. I will be using it more often.
What about you? Do you use Option Compare? What are your favorite tips & tricks? Please share with us using comments.
More on Excel VBA
VBA (or Macros) is how you can tell Excel to automate parts of your work. It is a powerful programming language built right in to Excel (and other MS Office applications) to help you do more. If you are new to VBA, why don’t you go thru our Free crash course?
- Introduction to VBA & Excel Macros
- Understanding Variables, Conditions & Loops in VBA
- Using Cells, Ranges & Other Objects in your Macros
- Putting it all together – Your First VBA Application using Excel
- My Top 10 Tips for Mastering VBA & Excel Macros
Also, go thru our VBA (Macros) article collection for more tips, tutorials & ideas.













3 Responses to “How-to create an elegant, fun & useful Excel Tracker – Step by Step Tutorial”
Hi Chandoo,
I am responsible for tracking when church reports are submitted on time or not and the variations from the due date for submission.
Here is the Scenario;
The due date for the submission of monthly reports is on the 5th of each month. and I would like to know how many reports have been submitted on time (i.e, those that have been submitted on or before the due date) I would also want to track those reports that have been submitted after the due date has passed.
How can I create such a tracker?
Hi Chandoo,
I am a member of your excel school.
I was trying to create SOP Tracker I follow all your steps but I keep this error below.
The list source must be a delimited list, or a reference to a single row or cell.
I try looking on YouTube for answer but no luck.
can you help on this?
thanks
Carl.
Dear Mr. Chando,
Rakesh, I'm working in a private company in the UAE. Recently, I'm struggling to get more details about the staff sick, annual, unpaid, and leaves. I would like to get a tracker in excel. Could you please help me in this situation?
I also watching your videos in YouTube. i hope you can help me on this situation.