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

时间:2016-06-15 20:59:09

标签: excel excel-vba excel-2010 copy-paste vba

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

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的不同变体,如果找到一个,它将返回TRUE或FALSE(false将是NAN,由IsNumeric捕获并转为FALSE),让我知道它检测到它。但是,这似乎并没有起作用。

任何人都可以查明我的错误吗?

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

2 个答案:

答案 0 :(得分:1)

您应该使用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

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

为了避免不同的情况(如@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

OR

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

OR

使用模块顶部的Option Compare Text,如下所示: https://msdn.microsoft.com/en-us/library/office/gg278697.aspx

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

Option Explicit

(为了良好的编码实践)。

答案 1 :(得分:1)

不确定您要使用j变量尝试完成的任务,因为它似乎没有任何相关性。除非我似乎在您的代码中发现错误并且Ralph提供了答案。 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