【发布时间】:2015-11-03 23:39:40
【问题描述】:
我想在其他字符串的集合(在 Excel 列中列出)中搜索以某个短语结尾的子字符串。
假设我想在其他字符串中搜索字符串“BLUE MOON”,并且我想确定子字符串在“BLUE MOON”的“N”之后结束,以避免结果为 TRUE,例如“蓝月光”。换句话说,我需要的是搜索单词的任何部分,但仅限于左手。右手边应该有一个固定的边框,即零个附加字符。 另一方面,如果开头不同,我需要积极的结果,例如“DARK BLUE MOON”应该导致 TRUE。因此,完全平等不是一种选择。
我想使用Find,但我相信这是不可能的。 Find似乎不接受除*之外的任何通配符。
这里有一些词供你测试:
-
预期搜索结果为阳性:
BLUE MOON DARK BLUE MOON LIGHT BLUE MOON -
预期的搜索结果是否定的::
BLUE MOONLIGHT LAST BLUE MOONSHINE BLUE MOONDANCE
任何提示也表示赞赏。现在我正在使用以下函数来删除单词(工作正常,除了它还删除了前面提到的带有负面预期搜索结果的案例):
Sub testingXXX()
Dim ws As Worksheet
Set ws = ActiveWorkbook.ActiveSheet
Dim aCell As Range, bCell As Range, aSave As String, y As Long
MyAr = Split("*BLUE MOON", ",")
For y = LBound(MyAr) To UBound(MyAr)
With ws
Set aCell = .Columns(1).Find(what:=MyAr(y), LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aSave = aCell.Address
Do
If bCell Is Nothing Then
Set bCell = .Range("A" & aCell.row)
Else
Set bCell = Union(bCell, .Range("A" & aCell.row))
End If
Set aCell = .Columns(1).FindNext(after:=aCell)
Loop Until aCell.Address = aSave
End If
Set aCell = Nothing
End With
Next y
If Not bCell Is Nothing Then bCell.EntireRow.Delete
End Sub
【问题讨论】:
标签: string vba search text wildcard