Reputation: 11
I am trying to search for all instances in a list for entries which match or begin with those listed in a criteria group. Imagine a list of servers consisting of the below. Where we have a database on that server also, it is indicated with an _SQL, _ORACLE suffix:
I need to search for all servers from the group below
I need to be able to identify the 2 listings of VM002 and 3 listings of VM003 in the list in Column A.
I can easily search for how many times VM002 or VM003 appear in the overall list by using the function, =COUNTIF(A:A,D1&"*")
.
What I need then is some way of indicating which servers in Col A match those of the smaller group, so I can sort, filter, etc. How can I identify entries in Col A along with their suffixes?
Upvotes: 0
Views: 262
Reputation: 96753
First enter the following User Defined Function (UDF) in a standard module:
Public Function Keyword(r1 As Range, r2 As Range) As Variant
v1 = r1.Text
For Each r In r2
If InStr(v1, r.Text) = 1 Then
Keyword = 1
Exit Function
End If
Next r
Keyword = ""
End Function
Then in B1 enter:
=keyword(A1,$D$1:$D$2)
and copy downward:
User Defined Functions (UDFs) are very easy to install and use:
If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx
To remove the UDF:
To use the UDF from Excel:
=myfunction(A1)
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
and for specifics on UDFs, see:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
Macros must be enabled for this to work!
EDIT#1:
To use the UDF for a full column, use this version instead:
Public Function Keyword(r1 As Range, r2 As Range) As Variant
v1 = r1.Text
Set r2 = Intersect(r2, r2.Parent.UsedRange)
For Each r In r2
If r.Value <> "" Then
If InStr(v1, r.Text) = 1 Then
Keyword = 1
Exit Function
End If
End If
Next r
Keyword = ""
End Function
Upvotes: 1