Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB / VBA StrComp or =

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.

like image 514
Barry-Jon Avatar asked Apr 16 '09 13:04

Barry-Jon


People also ask

What is StrComp in VBA?

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.

How do I compare two strings in VBA?

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.

What is the purpose of StrComp () string function?

The StrComp function compares two strings and returns a value that represents the result of the comparison.


2 Answers

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.

like image 57
Konrad Rudolph Avatar answered Oct 02 '22 01:10

Konrad Rudolph


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)...

like image 32
gbianchi Avatar answered Oct 02 '22 00:10

gbianchi