What, if anything, is the benefit of using
If StrComp(strVal1, strVal2, vbTextCompare) = 0 Then
as opposed to using
If strVal1 = strVal2 Then
If Option Compare Text
is set at the module level, is there any difference?
I know StrComp handles null scenarios and <> scenarios, I am only interested in the situation where strVal1 and strVal2 have non-null valid strings assigned.
The Microsoft Excel STRCOMP function returns an integer value representing the result of a string comparison. The STRCOMP function is a built-in function in Excel that is categorized as a String/Text Function. It can be used as a VBA function (VBA) in Excel.
Now, follow the below steps to compare strings in VBA. Step 1: Define sub-procedure which can hold your macro. Step 2: Define a variable Result as String so that we can assign a value of StrComp function to it. Step 3: Now use the Assignment operator to assign the value of StrComp to the variable named Result.
The StrComp function compares two strings and returns a value that represents the result of the comparison.
If
Option Compare Text
is set at the module level, is there any difference?
No. It simply offers a finer grained control (no module-level strategy commitments). However, if you can make such a commitment, go for the x = y
option: less code is always better code.
Since StrComp is comparing string (with cultural info), UpperCase and LowerCase are not taking care ... (so Hello is the same as hello). In the case of =, there will be different (Like using a Binary compare). If option compare text is at module level, there will be no difference (but you should use StrComp in case another guy delete it)...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With