如何在Excel的过滤器包含选项中将变量作为输入

时间:2015-02-12 15:23:43

标签: excel-vba vba excel

我试图通过excel VBA使用以下代码从句子中找到确切的单词。



Dim col As Range, cell1 As Range, a As String, i As Integer
Set col = Range("KW[KW1]")
Dim target, cell As Range
Sheets("Data").Select
Set target = Range(Range("B1"), Range("B65536").End(xlUp))
Dim term, tag As String
    For Each cell1 In col
    a = cell1.Value
    term = a
    tag = a

        For Each cell In target
            
            
            If InStr(1, cell, term, 1) Then
            
                For i = 1 To 50
                    If cell.Offset(0, i).Value = "" Then
                    cell.Offset(0, i).Value = tag
                    GoTo Step1
                    End If
                Next i

        End If
Step1:
        Next cell
    Next cell1
          
End Sub




但是它给了#34; wood"来自" Rosewood"这是错的。如何找到准确的单词" wood"

1 个答案:

答案 0 :(得分:0)

进行精确单词搜索的最简单方法是使用空格包围搜索文本和您要查找的单词。使用你的代码就可以了:

If InStr(1, " " & cell & " ", " " & term & " ", 1) Then

这样就不会在“Rosewood”中找到“木头”