Home » excel » excel – Finding specific string typed together with other characters

excel – Finding specific string typed together with other characters

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am trying to verify if a number (7) or string (‘7’) exist in one of the columns, and if they do to return a message box. For example, consider the following data:

Name    Cod
A       7
A       3
B       4
C       7;3
D       7 3
D       7, 3
F       7 - 3 
F       5

I can find the number 7 in the column Cod, but cannot check when it comes together with other numbers and symbols like comma, dash, semicolon, etc. The following code works for cell B2:

Dim N As Range

Sub Find_7_string()

    msg = ""

    For Each N In Range("A2", Range("A2").End(xlDown))
        If N.Offset(, 1) = 7 Then
        msg = msg & "There should not be 7 in column Cod" & vbLf
            Exit For
        End If
    Next N

    If Len(msg) > 1 Then
        MsgBox msg
    End If

End Sub

But it doesn’t work for cells B5, B6, B7 and B8.

How could I also verify the character ‘7’ when it is a string typed together with other characters?

How to&Answers:

You can use the Instr function, like this:

Sub Find_7_string()
Dim N As Range
Dim msg As String

msg = ""
For Each N In Range("A2", Range("A2").End(xlDown))
    If InStr(N.Offset(, 1), 7) Then
        msg = msg & "There should not be 7 in column Cod" & vbLf
        Exit For
    End If
Next N

If Len(msg) > 1 Then
    MsgBox msg
End If
End Sub

Note that is will also find 77, 7,382, etc.

Answer:

manual

  • Data ….. Filter
  • click on Filter dropdown
  • Number Filer …… Custom Filter
  • contains 7 Or equals 7

vba

Sub Findem()
Dim rng1 As Range
ActiveSheet.AutoFilterMode = False
Set rng1 = Range([b1], Cells(Rows.Count, "B").End(xlUp))
rng1.AutoFilter Field:=1, Criteria1:="=*7*", Operator:=xlOr, Criteria2:="=7"
End Sub