2016-06-15 32 views
0

我有一些代码我正在处理的地方,我需要检测一个单元格是否在其中包含特定的单词,如果是,它会在相邻的单元格中插入一个特定的字符串。但是,我遇到了检测部分的问题!这是迄今为止我所拥有的。如何检测一个单词是否存在于一个单元格中,在一个字符串内?

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)字气缸的不同变化,如果找到一个,它会返回TRUE或FALSE(假将是一个NAN ,被IsNumeric捕获并转化为FALSE),并让我知道它检测到它。但是,这似乎并不奏效。

有人能指出我的错误吗?

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

+1

你应该使用'InStr'的方法做这样的比较:'如果InStr函数(1,电池(7,J), “拉缸”)> 0或者InStr函数(1,单元格(7,j),“CYLINDERS”)> 0或InStr(1,单元格(7,j),“CYL”)> 0 Then' – Ralph

+1

此外,当“Cyl”,“cyl”?你为什么不把代码中的单词改为大写来覆盖这些场景-UCase() - ?或者在模块的开头使用Option Compare Text,我不明白为什么要做3次比较,如果单元格有“CYL”,当然它会有CYLINDER和CYLINDERS。 – Sgdva

+0

太好了,谢谢拉尔夫。你是正确的Sgdva。 @Ralph,如果你提交这个评论,我会将其标记为答案。 – TheTreeMan

回答

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 

欲了解更多关于此功能在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 

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 

(为了良好的编码习惯)。

+0

谢谢你的帮助!我很感激:) – TheTreeMan

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 
+0

j是我如何追踪别的东西。这与当前的代码并不太相关。谢谢你把它放在一起! – TheTreeMan

相关问题