How to use Option Compare statement in VBA?

The Option Compare statement in VBA (Visual Basic for Applications) is used to specify how string comparisons are made in a VBA module. This statement sets the default comparison method for string comparisons, and it must be placed at the top of a module, before any procedures.

There are two types of string comparisons in VBA, which can be set using Option Compare:

Option Compare Binary

This is the default comparison method if Option Compare is not specified. In binary comparisons, strings are compared based on the binary value of each character, which is derived from the character’s ASCII or Unicode value. This type of comparison is case-sensitive.

Option Compare Text

In text comparisons, strings are compared in a case-insensitive manner, and it takes into account the locale settings of your system. This is useful when you want to compare strings in a way that ignores case, or when comparing strings in languages that have case-insensitive semantics.

Here’s how to use Option Compare:

Place at the Top of the Module

Like Option Base, the Option Compare statement must be placed before any procedures in a module.


  • Option Compare Binary: Enables binary comparison.
  • Option Compare Text: Enables text (case-insensitive) comparison.


The Option Compare setting only affects the module in which it is placed. Each module can have its own Option Compare setting.


Without Option Compare or with Option Compare Binary:

' Strings "abc" and "ABC" are considered different
If "abc" = "ABC" Then MsgBox "Equal" Else MsgBox "Not Equal"

With Option Compare Text:

Option Compare Text
' Strings "abc" and "ABC" are considered the same
If "abc" = "ABC" Then MsgBox "Equal" Else MsgBox "Not Equal"

Consistency and Readability: Consistency in the use of Option Compare across different modules is important for maintaining readability and preventing confusion in your project.

Remember, the Option Compare setting can significantly affect the behavior of string comparison operations like =, <, >, <=, >=, Like, and string functions such as StrComp. It’s crucial to choose the appropriate comparison method based on your specific needs and the requirements of your project.

Unlock Your Potential


Basic - Advanced


Access Basic - Advanced

Power BI

Power BI Basic - Advanced

Help us grow the project

Leave a Reply

Your email address will not be published. Required fields are marked *