如何找到确切的Instr值

时间:2015-09-17 21:55:22

标签: excel vba excel-vba

我正在尝试查找确切的数字而不是查找整数。这样的事情 456 678 。每当我运行代码时,我最终都会找到整数( 45677777 )而不是 456 。为了说清楚,我在同一行中的数字很少( 456,456777777,677,456,456666666 等等)。我正在尝试使用InStr来避免 4567777 中的数字。

enter image description here

代码

 dim b as string 
   b = "-456-"
    for i = 1 to lastrow
     if instr( 1, worksheets("sheet1").range("A" & i), b > 0 then
      msgbox yes
     end if 
  next i

4 个答案:

答案 0 :(得分:4)

如果您想要完全匹配而不是InStr,则应使用=运算符进行比较。

  b = "456"
    For i = 1 To 7
     If (b = Cells(1, i).Value) Then
      Debug.Print ("Yes")
     Else
      Debug.Print ("No")
     End If
  Next i

如果您尝试测试的值具有这些连字符,那么在进行任何比较之前,您必须将它们解析出来。

此外,如果您正在使用范围,我发现将范围导入数组并使用数组而不是实际单元格,它们的值更容易也更快。编辑:为了使它更具动态性,所以你不必硬编码你正在寻找的范围是什么,并且你希望sub做到这一点,直到列中没有要查看的值,你可以使用Find方法。

这样可以适用于您的示例:

Dim arrVals()
lastrow = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
strRangeToCapture = "A1:A" & CStr(lastrow)
arrVals = Range(strRangeToCapture)

Dim b As String
b = "456"

For i = 1 To lastrow
    If (b = arrVals(i, 1)) Then
        Debug.Print ("Yes")
    Else
        Debug.Print ("No")
    End If
Next

希望这有帮助! :)

答案 1 :(得分:1)

您的示例数据无法显示,但从您的说明中可以看出,您的单元格中包含逗号分隔值。

从单元格中拆分值,然后查看每个值以获得完全匹配。

ColumnA
---------------------
444, 456777, 456, 888
777
444, 456777, 444, 888
456   

这只会找到第1行和第4行。

Private Sub CommandButton5_Click()
    Dim lRow As Long
    Dim ws As Excel.Worksheet
    Dim strValues() As String
    Dim i As Integer
    Dim b As String

    b = "456"

    Set ws = Application.ActiveSheet

    'Loop through all the rows.
    lRow = 1
    Do While lRow <= ws.UsedRange.Rows.Count

        'Get the values
        strValues = Split(ws.Range("A" & lRow).Value, ",")

        For i = 0 To UBound(strValues)
            If strValues(i) = b Then
                MsgBox ("Row " & Str(lRow) & " has a match.")
            End If
        Next i

        lRow = lRow + 1
    Loop
End Sub

答案 2 :(得分:0)

来自MSDN,InStr

  

返回一个整数,指定第一次出现在另一个

中的一个字符串的起始位置

所以你观察到的是所述功能的正确行为 为了得到你想要的东西,你可以使用理查德发布的内容,或者自从使用Excel以来,你可以尝试使用范围对象的Find方法,它在大数据集的速度方面有一些好处。

类似的东西:

Dim r As Range, found As Range
Dim b As String, FAdd As String
Dim lastrow As String

With Worksheets("Sheet1")
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    Set r = .Range("A1:A" & lastrow): b = "456"
    Set found = r.Find(b, .Range("A" & lastrow) , , xlWhole)
End With

If Not found Is Nothing Then FAdd = found.Address Else MsgBox "No match found.": Exit Sub

Do
    Debug.Print found.Address
    Set found = r.FindNext(After:=found)
Loop While FAdd <> found.Address

答案 3 :(得分:0)

尝试这种方法:

数据样本:

enter image description here

代码:

Sub test()
Dim cl As Range, key As Variant, Data As Range, criteria$
criteria = "456"
Set Data = Range([A1], Cells(Cells(Rows.Count, "A").End(xlUp).Row, "A"))
For Each cl In Data
    For Each key In Split(cl.Value, ",")
        If Trim(key) = criteria Then
            MsgBox "Row: " & cl.Row & " has a value: " & cl.Value & " matched with search string"
        End If
    Next key
Next cl
End Sub