在VBA中判断两个字符串是否相等,可以使用StrComp
函数或直接使用=
运算符。以下是两种方法的示例:
- 使用
StrComp
函数:
SubCompareStrings()
Dimstr1AsString
Dimstr2AsString
str1="Hello"
str2="hello"
IfStrComp(str1,str2,vbTextCompare)=0Then
MsgBox"Stringsareequal."
Else
MsgBox"Stringsarenotequal."
EndIf
EndSub
=
运算符:SubCompareStrings()
Dimstr1AsString
Dimstr2AsString
str1="Hello"
str2="hello"
Ifstr1=str2Then
MsgBox"Stringsareequal."
Else
MsgBox"Stringsarenotequal."
EndIf
EndSub
注意:在使用StrComp
函数时,第三个参数vbTextCompare
表示忽略大小写进行比较。如果希望区分大小写,可以使用vbBinaryCompare
参数。