It is necessary that when comparing two similar strings.

For example: If "yutsuke" contains / includes "ytsu" = true.

In others, there is such a method "contains", but in VBA, how?

    3 answers 3

    In VBA, there is an InStr function for this purpose. Used as

    InStr([start,] string1, string2[, compare]) 

    where start is an optional parameter indicating the starting position for the search (by default from the first character)
    string1 - search string
    string2 - the desired string
    compare is an optional parameter that specifies the comparison method (binary, text, or database information comparison (Access only)). The default is a binary comparison.

    Return Values:
    NULL - if any of the rows is NULL ,
    0 - if string1 empty string, or string2 not found, or start greater than the length of string1 ,
    start - if string2 empty string,
    if string2 found inside string1 , then returns the position at which a match was found.

    Usage example

     Function contains(string1 As String, string2 As String, Optional start As Integer = 1, _ Optional compare As VbCompareMethod = vbTextCompare) As Boolean ' функция проверки вхождения строки string2 в строку string1 без учёта регистра ' если string2 - пустая строка, то возвращается False Dim findPosition As Integer contains = False findPosition = InStr(start, string1, string2, compare) If Len(string2) > 0 And findPosition >= start Then contains = True End Function Sub test() Dim str1 As String, str2 As String Dim start As Integer str1 = "absqwerty" str2 = "qwer" MsgBox contains(str1, str2) End Sub 

      In addition to the answer already published:

      You can use the like operator:

       if "йцуке" like "*йцу*" then msgbox "Содержит" else msgbox "Не содержит" end if 

      This operator is case sensitive, so if you need to lower the sensitivity, you can use one of two options:

      1. Option Compare Text
      2. Use Lcase or Ucase , i.e. your example might look like this:

        if Lcase("йЦуКе") like Lcase("*ЙцУ*") then ....

        I repeat, but still. InStr, Like - for the string. If the array is a string, then Filter is used.

        Filter (<Strings array>, <String Search> [, <inclusion>] [, <comparison>]) - looks through the array of string values ​​and searches in it for all substrings that match the specified string. This function has four arguments: <string Search> - the search string; <inclusion> - a parameter (boolean value) that indicates whether the returned strings include the required substring or, on the contrary, only those array lines that do not contain the strung as a substring will be returned; <compare> is a parameter that defines the string comparison method.