Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm looking to create a function in Excel/VBA that will look at a range of cells and return a value of TRUE if any of the cells contains a certain character (an asterisk *). There will be some blank cells, some cells will contain text and some may contain text and an asterisk. Can anyone help? Many thanks
Posted

1 solution

Hello,

You may use the following formula on a worksheet:

=NOT(ISERROR(MATCH("*";$A$1:$A$10;0)))

But, "~" must be specified as "~~". For example:

=NOT(ISERROR(MATCH("~~";$A$1:$A$10;0)))

VBA functions:

VB
Function MyMatch(ByVal lookup_value As Variant, ByVal lookup_range As Variant) As Long

On Error Resume Next

Dim index As Long

index = InStr(1, lookup_value, "~", vbBinaryCompare)

If index > 0 Then
    lookup_value = Replace(lookup_value, "~", "~~")
End If

MyMatch = Application.Match(lookup_value, lookup_range, 0)

End Function

Function HasValue(ByVal lookup_value As Variant, ByVal lookup_range As Variant) As Boolean

HasValue = (MyMatch(lookup_value, lookup_range) > 0)

End Function
 
Share this answer
 
Comments
Maciej Los 29-Nov-13 8:11am    
Good job!
+5!
Raja Sekhar S 2-Dec-13 2:54am    
Nice one.. +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900