'********************************************************************************************************** ' Name: containsAny ' Author: mielk | 2013-05-16 ' ' Comment: Checks if the given string contains any of substrings given in parameter [strings]. ' ' Parameters: ' isCaseSensitive Parameter defining if text matching is case sensitive. ' In most functions this parameter is optional, however since there is a ParamArray ' parameter in this function, it is not allowed to define this argument as optional. ' baseString Base string to be checked ' strings Array of strings that function will be look for in the base string. ' ' Returns: ' Boolean True - if base string contains any of substrings declared in [strings] param array. ' False - otherwise. ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2013-05-16 mielk Class created. '********************************************************************************************************** Public Function containsAny(isCaseSensitive As Boolean, baseString As String, _ ParamArray strings() As Variant) As Boolean Const METHOD_NAME As String = "containsAny" '------------------------------------------------------------------------------------------------------ Dim varText As Variant Dim uCompareMethod As VBA.VbCompareMethod '------------------------------------------------------------------------------------------------------ 'Convert [isCaseSensitive] parameter of Boolean type to the [VbCompareMethod] enumeration. ----------| If isCaseSensitive Then '| uCompareMethod = VBA.vbBinaryCompare '| Else '| uCompareMethod = VBA.vbTextCompare '| End If '| '----------------------------------------------------------------------------------------------------| 'Iterate through all the substrings declared in [strings] param array. ------------------------------| For Each varText In strings '| '| 'Check if this substring is contained in base string. ---------------------------------------| '| If VBA.InStr(1, baseString, stringify(varText), uCompareMethod) Then '| '| '| '| containsAny = True '| '| '| '| 'If any substring has been found in the base string, there is no point to check '| '| 'the rest of substring, since function will return True anyway. That is why the '| '| 'function leaves this loop once first substring is found. '| '| Exit For '| '| '| '| End If '| '| '--------------------------------------------------------------------------------------------| '| '| Next varText '| '----------------------------------------------------------------------------------------------------| End Function '********************************************************************************************************** ' Name: stringify ' Author: mielk | 2014-09-20 ' ' Description: Function to convert the given parameter into String. ' This function works similar to VBA built-in function VBA.CStr but it doesn't throw ' an error if value passed as a input parameter cannot be converted to String. ' ' ' Parameters: ' value Value to be converted into String. ' ' ' Returns: ' String The string representation of the given parameter. ' * For primitives value, function returns the same value as VBA built-in function ' VBA.CStr. ' * For arrays, function returns all its values converted to String. ' * For object, function check if it contains function toString and returns its ' result. If there is no such method for this object, value defined in OBJECT_TAG ' constant is returned. ' ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2014-09-20 mielk Function created. '********************************************************************************************************** Public Function stringify(value As Variant) As String Const METHOD_NAME As String = "stringify" '------------------------------------------------------------------------------------------------------ Const OBJECT_TAG = "[Object]" '------------------------------------------------------------------------------------------------------ On Error Resume Next 'For missing and empty parameters, empty String is returned. ----------------------------------------| If Not VBA.IsMissing(value) And Not VBA.isEmpty(value) Then '| '| 'There is different logic for obtaining String representation of object and -----------------| '| 'primitive value, so function needs to check which one is the given parameter. '| '| If VBA.IsObject(value) Then '| '| stringify = value.toString '| '| If VBA.Len(stringify) = 0 Then stringify = OBJECT_TAG '| '| Else '| '| stringify = VBA.CStr(value) '| '| End If '| '| '--------------------------------------------------------------------------------------------| '| '| End If '| '----------------------------------------------------------------------------------------------------| End Function