InStr reloads with Variant options
The InStr function has 4 optional parameters at design time, but at least 2 arguments must be provided at run time. The first 3 parameters for InStr are all Variant , which allows InStr support two different syntaxes and effectively simulate an overloaded function. One of the reasons String1 and String2 is defined as Variant and not as String types. Start may be Long , but it is also a Variant type.
In the following 4 examples, x always assigned the value 4
Option 1 - Using the specified order of parameters or name values
The signature of the function behaves as it is defined:
InStr Function ([Start], [String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])
x = VBA.InStr(1, "food", "D", vbTextCompare) '4 x = VBA.InStr(Start:=1, String1:="food", String2:="D", Compare:=vbTextCompare) '4
Option 2 - Use an alternate order or name value
The signature of the function behaves as if it were defined as:
InStr Function ([String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])
This means that Start should be used as if it were String1 and String1 should be used as if it were String2 . String2 argument must be omitted or you get a Type Mismatch error.
x = VBA.InStr("food", "D", , vbTextCompare) '4 x = VBA.InStr(Start:="food", String1:="D", Compare:=vbTextCompare) '4
Using named parameters
But, as you discovered, the InStr function suffers from syntax and / or compilation errors when using named parameters:
Syntax error: expected list delimiter
When all parameters are named:
x = InStr(Start:=1, String1:="foo", String1:="foo", Compare:=vbBinaryCompare)
You are getting:
Syntax Error: Expected List Segmenter
Compilation error: object does not support named arguments
When some of the parameters are called:
x = InStr(1, String1:="foo", String2:="foo", Compare:=vbBinaryCompare)
You are getting:
Compilation error: Object does not support named arguments
The same errors with the StrComp function
The StrComp function StrComp not have functions of an overloaded type, but it has the same problems with syntax and compilation errors:
x = StrComp(String1:="foo", String2:="foo", Compare:=vbBinaryCompare) 'Syntax Error: Expected List Separator??? x = StrComp("foo", String2:="foo", Compare:=vbBinaryCompare) 'Compile error: Object doesn't support named arguments
But, as OP discovered, an error does not occur with InStrRev .
So, what is common between InStr and StrComp , which is different from InStrRev and, apparently, all other VBA functions?
Well, InStr and StrComp both use these functions:
- Functions defined in the first type reference library
- Functions defined in the TLB / COM module
- All but the latest options are
Variant . - The final parameter is
Enum with a default value. - The return value is an option.
I cannot find any other functions in the VBA library that share these characteristics, so I suspect there is a compilation error associated with these characteristics.
Qualification of the function fixes the problem!?!?
Both InStrRev and StrComp can be used with all / some named parameters if the function is defined by the library name or module name:
'InStr Vanilla usage: x = Strings.InStr(Start:=1, String1:="food", String2:="D", Compare:=vbTextCompare) '4 x = VBA.InStr(Start:=1, String1:="food", String2:="D", Compare:=vbTextCompare) '4 'InStr Alternate usage: x = Strings.InStr(Start:="food", String1:="D", Compare:=vbTextCompare) '4 x = VBA.InStr(Start:="food", String1:="D", Compare:=vbTextCompare) '4 'StrComp usage x = Strings.StrComp(String1:="food", String2:="D", Compare:=vbTextCompare) '1 x = VBA.StrComp(String1:="food", String2:="D", Compare:=vbTextCompare) '1