在string中find具体的数字?

这是我的数据的示例,其中name位于单元格A1 。 C列不是数据的一部分,只是为了说明想要的内容。

 name cod should be detected? aa no aa 14;15 no aa 1;13;7 yes bb 8;9;1 yes bb 1;17 yes bb 11;21 no cz 7;8 no cz 7;21 no cz 8;1;20 yes db 1 yes db 13;1 yes 

我试图写一个macros来检测列1号出现的列cod 。 例如,我不想find10,13,21,但是1 。 本专栏填写的数字从1到21。

所有的cod值都是string,但我想find哪里有1 ,即使它出现在string中的其他数字混合。 这一列中的数字总是与之隔开; 两者之间没有空白。

以下代码会产生误报:

 Dim N As Range Dim msg As String Sub cod1() msg = "" For Each N In Range("A2", Range("A2").End(xlDown)) If InStr(1, N.Offset(, 1), 1, vbTextCompare) > 0 Then msg = msg & "Code 1 was not supposed to be in Cod column." & vbLf Exit For End If Next N If Len(msg) > 1 Then MsgBox msg Else: MsgBox "There are no code 1 values in Cod column." End If End Sub 

查看结果:

 name cod should be detected? problem aa no aa 14;15 no false positive aa 1;13;7 yes bb 8;9;1 yes bb 1;17 yes bb 11;21 no false positive cz 7;8 no cz 7;21 no false positive cz 8;1;20 yes db 1 yes db 13;1 yes 

下面的代码会产生错误的否定

 Dim N As Range Dim msg As String Sub cod2() msg = "" For Each N In Range("A2", Range("A2").End(xlDown)) If InStr(1, N.Offset(, 1), 1, vbTextCompare) > 0 And _ InStr(1, N.Offset(, 1), 10, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 11, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 12, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 13, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 14, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 15, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 16, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 17, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 18, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 19, vbTextCompare) = 0 And _ InStr(1, N.Offset(, 1), 21, vbTextCompare) = 0 Then msg = msg & "Code 1 was not supposed to be in Cod column." & vbLf Exit For End If Next N If Len(msg) > 1 Then MsgBox msg Else: MsgBox "There are no code 1 values in Cod column." End If End Sub 

查看结果:

 name cod should be detected? problem aa no aa 14;15 no aa 1;13;7 yes false negative bb 8;9;1 yes bb 1;17 yes false negative bb 11;21 no cz 7;8;10 no cz 7;21 no cz 8;1;20 yes false negative db 1 yes db 13;1 yes false negative 

那么,如何才能使消息框*仅在string内检测到数字1时出现?

*代码1不应该在Cod列中。


寻找一种适用于Excel 2007及更新版本的解决scheme。

您可以使用Like运算符来查找字符:

 Dim N As Range Dim msg As String Sub cod1() Dim expression As String msg = "" For Each N In Range("A2", Range("A2").End(xlDown)) expression = ";" & N.Offset(, 1) & ";" If expression Like "*;1;*" Then msg = msg & "Code 1 was not supposed to be in Cod column." & vbLf End If Next N If Len(msg) > 1 Then MsgBox msg Else MsgBox "There are no code 1 values in Cod column." End If End Sub 

这个解决scheme使用Split函数在B列中生成一个数组值,然后比较每个数组项。

 Sub Test() Dim rDta As Range, rRow As Range Dim aRow As Variant, vItm As Variant Dim sMsg As String, lRow As Long With ThisWorkbook.Sheets("DATA.3").Cells(1).CurrentRegion 'change as required Set rDta = .Offset(1).Resize(-1 + .Rows.Count) End With lRow = 1 For Each rRow In rDta.Rows lRow = 1 + lRow aRow = Split(rRow.Cells(2).Value2, ";") For Each vItm In aRow If vItm = 1 Then If sMsg = vbNullString Then sMsg = "Code 1 was not supposed to be in Cod column of rows:" sMsg = sMsg & vbLf & vbTab & lRow rRow.Cells(1, 3).Value = "Code 1 was not supposed to be in Cod column." 'Remove if required End If: Next: Next If sMsg = vbNullString Then sMsg = "There are no code 1 values in Cod column." MsgBox sMsg End Sub 

或者,您可以使用任何分隔符来检测单元格内是否存在任何值的通用UDF(用户定义函数):

 Public Function hasItem(ByVal r As Range, item As Variant, sep As String) As Boolean ar = Split(r.Text, sep) For Each x In ar If Trim(CStr(x)) = Trim(CStr(item)) Then hasItem = True Exit Function End If Next End Function 

把上面的UDF放在Module Module1代码中,并在C列的单元格中使用它,例如在C1

 =IF(hasItem(B1, 1, ";"), "yes", "no") 

然后你可以复制/粘贴到所有的C单元格中。 此外,您可以在任何VBA代码中方便地使用该function来显示所需的消息。