2016-06-15 33 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

相關問題