It seems you are right, in that vbUseCompareOption
does not exisit as a member of enumeration vbCompareMethod
![vbComapreMethod]()
So, what happens when you try and use it? As you found, when using Option Explicit
you get a compile error, because a variable of that name is not defined.
If you don't use Option Explicit
(usually a bad idea), a variable is created at run time, of type Variant
, and value Empty
. I ran an experiment to see what happens:
TL:DR - yes, you may get unexpected results (Binary Compare is always used)
I created two modules, one with Option Compare Binary
the other with Option Compare text
. I used StrComp
as a test function that uses a Compare parameter.
Module one
'Option Explicit
Option Compare Text
Sub DemoText()
Dim a As String, b As String
a = "AAA"
b = "aaa"
Debug.Print "Option Comapre Text"
Debug.Print "Text", "Binary", "Omitted", "vbUseCompareOption"
Debug.Print StrComp(a, b, vbTextCompare), StrComp(a, b, vbBinaryCompare), StrComp(a, b), StrComp(a, b, vbUseCompareOption)
End Sub
Module two
'Option Explicit
Option Compare Binary
Sub DemoBinary()
Dim a As String, b As String
a = "AAA"
b = "aaa"
Debug.Print "Option Comapre Binary"
Debug.Print "Text", "Binary", "Omitted", "vbUseCompareOption"
Debug.Print StrComp(a, b, vbTextCompare), StrComp(a, b, vbBinaryCompare), StrComp(a, b), StrComp(a, b, vbUseCompareOption)
End Sub
The test
Sub Demo()
DemoText
DemoBinary
End Sub
The Result
Option Comapre Text
Text Binary Omitted vbUseCompareOption
0 -1 0 -1
Option Comapre Binary
Text Binary Omitted vbUseCompareOption
0 -1 -1 -1
And if you assign a value of -1
to it, you get a run time error 5 "Invalid Procedure Call or argument"
So, vbUseCompareOption
does not exist, creating it with a value of -1
(or just calling a function with Compare:=-1
) as the help suggests, fails.
Letting VBA create it as a variable (with value Empty
) has no effect. Binary compare is always applied.
AFAICT, the only way to use the Option Compare
setting is to leave out the Compare parameter in a function call.
![enter image description here]()