Vba 比较两个忽略大小写的字符串变量
我需要比较If语句中的2个变量,但我仍然希望它在除情况外所有内容都相同的情况下都为true。我知道我不能写这篇文章,但以下是我想做的事情:Vba 比较两个忽略大小写的字符串变量,vba,excel,Vba,Excel,我需要比较If语句中的2个变量,但我仍然希望它在除情况外所有内容都相同的情况下都为true。我知道我不能写这篇文章,但以下是我想做的事情: If (str1=str2 matchcase:=false) then 有什么想法吗? 谢谢如果(lcase(str1)=lcase(str2)),那么尽管我更喜欢@mr.Reband-answer,但您仍然可以参考这个使用StrComp功能的替代方案 Sub test() Dim str1 As String Dim str2 As String
If (str1=str2 matchcase:=false) then
有什么想法吗?
谢谢
如果(lcase(str1)=lcase(str2)),那么
尽管我更喜欢@mr.Reband-answer,但您仍然可以参考这个使用StrComp
功能的替代方案
Sub test()
Dim str1 As String
Dim str2 As String
str1 = "test"
str2 = "Test"
MsgBox StrComp(str1, str2, vbTextCompare)
'Return Values
'
'The StrComp function has the following return values:
'
'If StrComp returns
'string1 is less than string2 -1
'string1 is equal to string2 0
'string1 is greater than string2 1
'string1 or string2 is Null Null
End Sub
如果您正在寻找最快的,则需要使用StrComp() StrComp将进行一些测试,以避免实际比较字符串或整个字符串,从而在字符串不同时更快。如果您确实希望使用很多通用值,那么可以避免使用StrComp,但在大多数情况下,它会更快
100M comparisons with same phrase but different case:
LCase = 20 seconds
StrComp = 23 seconds *This is a rare case
100M comparisons with different phrase but same length
LCase = 20 seconds
StrComp = 9 seconds
100M comparisons with different phrase and different length
LCase = 25 seconds
StrComp = 9 seconds
注意:字符串的长度将因结果而异。例如,在上一次测试中,LCase比其他测试花费的时间更长,因为我将其中一个字符串的长度增加了一倍。这减慢了LCase的速度,但没有降低StrComp的速度
注2:LCase和UCase的结果相似