Excel 如何检测字符串中的单元格中是否存在单词?
我有一些我正在处理的代码,我需要检测一个单元格中是否有一个特定的单词,如果有,它会在相邻的单元格中插入一个特定的字符串。但是,我在做检测部分时遇到了问题!这是我到目前为止所拥有的Excel 如何检测字符串中的单元格中是否存在单词?,excel,excel-2010,copy-paste,vba,Excel,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 V
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(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
有关此功能的更多信息,请访问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
或
或
使用模块顶部的选项Compare Text
,如下所述:
同时,您可能需要考虑插入行:
Option Explicit
(用于良好的编码实践)。您应该使用
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
为了避免不同的情况(如@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
或
或
使用模块顶部的选项Compare Text
,如下所述:
同时,您可能需要考虑插入行:
Option Explicit
(用于良好的编码实践)。不确定使用
j
变量试图实现什么,因为它似乎没有任何相关性。除了我在你的代码和拉尔夫提供的答案中发现了一个错误<代码>单元格(7,j)应该是单元格(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
不确定使用
j
变量试图实现什么,因为它似乎没有任何相关性。除了我在你的代码和拉尔夫提供的答案中发现了一个错误<代码>单元格(7,j)应该是单元格(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
您应该使用
InStr
方法进行如下比较:如果InStr(1,单元格(7,j),“圆柱体”)>0或InStr(1,单元格(7,j),“圆柱体”)>0或InStr(1,单元格(7,j),“圆柱体”)>0,那么
还有,“圆柱体”,“圆柱体”时会发生什么?为什么不在代码中将单词改为大写以涵盖这些场景-UCase()-?或者在模块开始时使用选项比较文本,我不明白为什么要进行3次比较,如果单元格有“CYL”,当然会有圆柱体和圆柱体。太好了,谢谢Ralph。你是对的,Sgdva@拉尔夫,如果你把它作为评论提交,我会把它标记为答案。你应该使用InStr
方法做这样的比较:如果InStr(1,单元格(7,j),“圆柱体”)>0或InStr(1,单元格(7,j),“圆柱体”)>0或InStr(1,单元格(7,j),“圆柱体”)>0,那么
还有,“圆柱体”,“圆柱体”时会发生什么?为什么不在代码中将单词改为大写以涵盖这些场景-UCase()-?或者在模块开始时使用选项比较文本,我不明白为什么要进行3次比较,如果单元格有“CYL”,当然会有圆柱体和圆柱体。太好了,谢谢Ralph。你是对的,Sgdva@拉尔夫,如果你把它作为评论提交,我会把它标记为答案。j是我追踪其他东西的方式。它与当前代码没有太大关系。谢谢你把它放在一起!j是我追踪其他事物的方式。它与当前代码没有太大关系。谢谢你把它放在一起!谢谢你的帮助!我很感激:)谢谢你的帮助!我很感激:)