在VBA中判断两个字符串是否相等,可以使用StrComp函数或直接使用=运算符。以下是两种方法的示例:
StrComp函数:Sub CompareStrings() Dim str1 As String Dim str2 As String str1 = "Hello" str2 = "hello" If StrComp(str1, str2, vbTextCompare) = 0 Then MsgBox "Strings are equal." Else MsgBox "Strings are not equal." End IfEnd Sub使用=运算符:Sub CompareStrings() Dim str1 As String Dim str2 As String str1 = "Hello" str2 = "hello" If str1 = str2 Then MsgBox "Strings are equal." Else MsgBox "Strings are not equal." End IfEnd Sub注意:在使用StrComp函数时,第三个参数vbTextCompare表示忽略大小写进行比较。如果希望区分大小写,可以使用vbBinaryCompare参数。




