0

Really inexperienced in VBA I'm trying to built a macro searching differents strings in multiple columns. I'm searching pattern like : "[ang]" "[fre]" "_[ger]

For now, I'm able to find _[ang] in column H, but I think there's a way to find all three patterns with one formula. Important part is to add a note in a specific cell (that's why I use cel.Offset(0, 4))

Sub testsearch()
    For Each cel In ActiveSheet.Range("H1:H" & Range("H" & Rows.Count).End(xlUp).Row)
        If cel Like "*_[ang]*" Then
            cel.Offset(0, 4) = "_[ang]"
        End If
    Next cel
End Sub
1
  • Like works for simple stuff. For anything non-trivial (like alternatives), look into regular expressions instead. Commented May 9, 2018 at 17:47

1 Answer 1

2

What about this..

Sub testsearch()

    Application.ScreenUpdating = False

    Dim string_patterns As Variant

    string_patterns = Array("_[ang]", "[fre]", "_[ger]")

    For Each cel In ActiveSheet.Range("H1:H" & Range("H" & Rows.Count).End(xlUp).Row)

        For Each element In string_patterns

            If cel Like "*" & CStr(element) & "*" Then
                cel.Offset(0, 4) = CStr(element)
                Exit For
            End If

        Next element

    Next cel

    Application.ScreenUpdating = True

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

You'll probably want to Exit For when a match is found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.