如何检测字符串中的单元格中是否存在单词?

How to detect if a word is present in a cell, within a string?

我有一些代码正在处理,我需要检测单元格中是否有特定单词,如果有,它会在相邻单元格中插入特定字符串。但是,我在执行检测部分时遇到问题!这是我目前所拥有的。

Sub searchandpaste()
    Dim stopvar As Variant
    Dim i As Variant
    Dim j As Variant
    Dim k As Variant
    Dim TestVal1 As Variant
    Dim TestVal2 As Variant

    i = 0
    j = 0

    Do While stopvar = 0
        i = i + 1
        MsgBox ("Row " & i)
        MsgBox ("j equals " & j)
        'If the first cell is empty, that means we've hit the end of the worksheet, and it stops the do-while loop
        TestVal1 = Cells(i, 1)
        If TestVal1 = 0 Then
            stopvar = 1
        Else
            TestVal2 = Cells(i, 6)
            If IsEmpty(TestVal2) = True Then
                MsgBox ("Detected Empty Cell in Column 6")
                j = 1
            ElseIf TestVal2 = "XXXX" Then
                'This means we have a place we need to insert a value
                MsgBox ("Detected XXXX in Column 6")
                'We know at this point that in Cells(6,i) we have a value we need to insert. Thus, we need to search Cells(7,i) for key text
                If IsNumeric(Cells(7, j).Find("CYLINDER")) Or IsNumeric(Cells(7, j).Find("CYLINDERS")) Or IsNumeric(Cells(7, j).Find("CYL")) = True Then
                    MsgBox ("Detected the string CYLINDER")
                    j = j + 1
                    MsgBox ("j equals " & j)
                Else
                    MsgBox ("Did not detect the string CYLINDER")
                End If

            End If
        End If
    Loop
End Sub

我把重要的部分删掉,在这里。

'We know at this point that in Cells(6,i) we have a value we need to insert. Thus, we need to search Cells(7,i) for key text
If IsNumeric(Cells(7, j).Find("CYLINDER")) Or IsNumeric(Cells(7, j).Find("CYLINDERS")) Or IsNumeric(Cells(7, j).Find("CYL")) = True Then
    MsgBox ("Detected the string CYLINDER")
    j = j + 1
    MsgBox ("j equals " & j)
Else
    MsgBox ("Did not detect the string CYLINDER")
End If

我的意图是,这将在单元格 (i,7) 中的字符串中搜索 Cylinder 一词的不同变体,如果找到一个,它将 return TRUE 或 FALSE(false 是NAN,它被 IsNumeric 捕获并变为 FALSE),让我知道它检测到它。但是,这似乎不起作用。

谁能指出我的错误?

有没有更好的搜索字符串的方法?比如,我可以只搜索 "CYL" 并让它说它检测到任何这些变化吗?

您应该使用 InStr 函数来进行这样的比较:

If InStr(1, Cells(7, j), "CYLINDER") > 0 Or _
    InStr(1, Cells(7, j), "CYLINDERS") > 0 Or _
    InStr(1, Cells(7, j), "CYL") > 0 Then
        MsgBox ("Detected the string CYLINDER")
        j = j + 1
        MsgBox ("j equals " & j)
Else
    MsgBox ("Did not detect the string CYLINDER")
End If

有关此功能的更多信息,请访问位于 https://msdn.microsoft.com/en-us/library/office/gg264811%28v=office.15%29.aspx

的 MSDN

为避免不同情况(如@Sgdva 所建议),您有多种选择:

If InStr(1, Cells(7, j), "CYLINDER", vbTextCompare) > 0 Or _
    InStr(1, Cells(7, j), "CYLINDERS", vbTextCompare) > 0 Or _
    InStr(1, Cells(7, j), "CYL", vbTextCompare) > 0 Then
        MsgBox ("Detected the string CYLINDER")
        j = j + 1
        MsgBox ("j equals " & j)
Else
    MsgBox ("Did not detect the string CYLINDER")
End If

If InStr(1, UCase(Cells(7, j)), "CYLINDER") > 0 Or _
    InStr(1, UCase(Cells(7, j)), "CYLINDERS") > 0 Or _
    InStr(1, UCase(Cells(7, j)), "CYL") > 0 Then
        MsgBox ("Detected the string CYLINDER")
        j = j + 1
        MsgBox ("j equals " & j)
Else
    MsgBox ("Did not detect the string CYLINDER")
End If

使用模块顶部的 Option Compare Text,正如此处指出的那样: https://msdn.microsoft.com/en-us/library/office/gg278697.aspx

同时,您可能要考虑插入以下行:

Option Explicit

(良好的编码习惯)。

不确定您要使用 j 变量完成什么,因为它似乎没有任何相关性。除了我似乎已经确定了您的代码和拉尔夫提供的答案中的错误。 Cells(7, j) 应该是 Cells(i, 7)。完整代码为:

Sub searchandpaste()
    Dim stopvar As Variant
    Dim i As Variant
    Dim j As Variant
    Dim k As Variant
    Dim TestVal1 As Variant
    Dim TestVal2 As Variant

    i = 0
    j = 0

    Do While stopvar = 0
        i = i + 1
        MsgBox ("Row " & i)
        MsgBox ("j equals " & j)
        'If the first cell is empty, that means we've hit the end of the worksheet, and it stops the do-while loop
        TestVal1 = Cells(i, 1)
        If TestVal1 = 0 Then
            stopvar = 1
        Else
            TestVal2 = Cells(i, 6)
            If IsEmpty(TestVal2) = True Then
                MsgBox ("Detected Empty Cell in Column 6")
                j = 1
            ElseIf TestVal2 = "XXXX" Then
                'This means we have a place we need to insert a value
                MsgBox ("Detected XXXX in Column 6")
                'We know at this point that in Cells(6,i) we have a value we need to insert. Thus, we need to search Cells(7,i) for key text
                If InStr(LCase(Cells(i, 7)), "cyl") > 0 Then
                    MsgBox ("Detected the string CYLINDER")
                    j = j + 1
                    MsgBox ("j equals " & j)
                Else
                    MsgBox ("Did not detect the string CYLINDER")
                End If

            End If
        End If
    Loop
End Sub