Here is an example of using an array (split from a constant string of known products, but could also be split from a variable string).
Const PRODUCT_NAMES As String = "Product1,Product2,Product3,Product4,Product5,Product6,Product10"
Sub Test()
Debug.Print FileNameMatch("123lkd_Product1kdalf") '<-- TRUE
Debug.Print FileNameMatch("Bob Smith Loves Product2") '<-- TRUE
Debug.Print FileNameMatch("P_R_O_D_UC_T_6") '<-- FALSE
Debug.Print FileNameMatch("Product54_Revised_01-01-2014") '<-- TRUE
End Sub
This calls on the function I created below. Basically this iterates each of the products from the list defined as PRODUCT_NAMES
. Since you used OR
operators, I simply tally the number of fuzzy matches in variable i
, any value of i >= 1
means there was at least one condition that matched, so the function returns True
. If there is no match, it returns false.
Function FileNameMatch(strName As String) As Boolean
Dim products() As String
Dim prod As Variant
Dim i As Integer
products = Split(PRODUCT_NAMES, ",")
For Each prod In products
If strName Like "*" & prod & "*" Then i = i + 1
Next
'Alternatively, you could use INSTR instead of Like
'For each prod in products
' If Instr(1, prod, strName) > 0 Then i = i + 1
'Next
FileNameMatch = (i >= 1)
End Function
Updated from comments
Sure, that would be one way to do it. Like,
If FileNameMatch("123kd_Product1_kdladfi") Then
'Call your formatting function, here:
ElseIf AnotherMatchingFunction("123kd_Product1_kdladfi" Then
'File name did not match the first list, but matches the second list
'Call a different formatting function, here:
End If