我有以下脚本matches any word in a cell to any other word in a range of cells。但是,我希望代码省略某些常用术语,例如"和,或者,大,小"。
我对VBA不太满意,所以我希望有人可以告诉我在代码中添加它的位置:
Sub FindSimilar()
Dim phrases As Range, phrase As Range
Dim terms As Range, term As Range
Dim matches As String
Dim words() As String
'ensure this has the correct sheet names for your workbook
Set phrases = ThisWorkbook.Worksheets("Export").Range("B2:B2000")
Set terms = ThisWorkbook.Worksheets("Topics").Range("D100:D3000")
For Each term In terms
matches = ""
words() = Split(term.Value)
For i = 0 To UBound(words, 1)
For Each phrase In phrases
If InStr(1, phrase.Value, words(i)) Then
matches = matches & phrase & "/"
End If
Next phrase
Next i
If matches <> vbNullString Then
term.Offset(0, 6).Value = Left(matches, Len(matches) - 1)
Else
term.Offset(0, 6).Value = "No match"
End If
Next term
End Sub
如何在代码中将术语列入黑名单?非常感谢您的积极投入和支持。
答案 0 :(得分:6)
如果我理解您的想法,可以在Select Case
For Each Loop
声明
For i = 0 To UBound(words, 1)
Select Case words(i)
Case = "and","or","big","small","whatever else you want to add"
Case Else
For Each phrase In phrases
If InStr(1, phrase.Value, words(i)) Then
matches = matches & phrase & "/"
End If
Next phrase
End Select
Next i
但是,如果有大量条款,这将变得难以处理。
如果有大量条款,您可以在工作表上存储BlackListed条款,然后执行以下操作:
For i = 0 To UBound(words, 1)
If ThisWorkbook.Worksheets("Topics").Range("BlackList").Find(words(i),lookat:=xlWhole) is Nothing Then
For Each phrase In phrases
If InStr(1, phrase.Value, words(i)) Then
matches = matches & phrase & "/"
End If
Next phrase
End If
Next i
假设BlackList是一个命名范围。如果不只是替换为A1:A100
或任何范围。