嘿,我有这行代码搜索匹配,我想考虑这两个字符串,例如相等:leve mur#1
和 leve mur
我找到了 like
和 Instr
之类的东西,但我对这个位置不感兴趣,我只是想让它忽略主题标签后面的内容
有什么想法吗?
Dim oHead As Long
oHead = wF.Match(LookingHead.Value, _
Range("A1", Cells(1, _
Range("A1").SpecialCells(xlCellTypeLastCell).Column)), False)
答案 0 :(得分:1)
我不知道您为什么不想使用 Instr
但这是我首先想到的解决这个特定问题的方法。当我使用 Instr
时,我从不关心在字符串中找到匹配项的位置。我只需要知道一个字符串是否包含另一个。
在您的情况下,您可能有一个较小的字符串和一个较大的字符串。在运行时可能不知道顺序,因此,只需以两种方式运行 Instr
以确保您捕获任何情况。
Sub Button1_Click()
Dim Str1 As String
Dim Str2 As String
Str1 = "leve mur#1"
Str2 = "leve mur"
If Contains(Str1, Str2) Then
MsgBox "They match"
Else
MsgBox "No match"
End If
End Sub
Function Contains(Str1 As String, Str2 As String) As Boolean
If Instr(Str1, Str2) > 0 Or Instr(Str2, Str1) > 0 Then
Contains = True
Else
Contains = False
End If
End Function
答案 1 :(得分:0)
为什么不使用:
if instr(1 str1, str2, vbTextCompare) > 0 then
'str2 has been found in str1
end if
使用相同的方法,如果您想忽略主题标签后面的内容...
hashPosit = InStr(1, str1, "#", vbTextCompare)
if hashPosit > 0 then
'We have a hash in this string, only take to the left
subStr1 = left(str1,hashPosit)
else
'No hash exists, take all string
subStr1 = str1
endif
if instr(1, subStr1, str2, vbTextCompare) > 0 then
'str2 has been found in the portion of str1 to the left of the #
end if
我想有点笨重,但你可以将它触发到一个函数中,它就足够干净了。