如何在字符串比较中忽略大小写

she*_*ngh 4 excel vba excel-2013

我有一个 Excel VBA 公式:-

If [G56] = "Not Applicable" Then
    ...
Run Code Online (Sandbox Code Playgroud)

它区分大小写。我希望它忽略“不适用”的情况。

jvm*_*eer 5

您可以只使用 LCase 函数:

If LCase([G56]) = "not applicable" Then
Run Code Online (Sandbox Code Playgroud)

  • 或“UCase$()”函数,只是为了论证;) (3认同)

Sam*_*Sam 5

您还可以使用专用函数来比较字符串:

Dim result As Integer

'// vbTextCompare does a case-insensitive comparison
result = StrComp("Not Applicable", "NOT APPLICABLE", vbTextCompare)

If result = 0 Then
    '// text matches
End If
Run Code Online (Sandbox Code Playgroud)

这篇 MSDN 文章中有关于该StrCompare方法的更多信息