How about the UDF approach? Instead of a macro, put this UDF code into a standard code module to give your workbook a new function you can use in a cell like any formula:
Option Explicit
Function AGELIST(MyRNG As Range, Age As Long, ListDir As String) As String
'To get ages above a threshold, used in a cell like: =AGELIST(A1:B10, 10, "OVER")
'To get ages below a threshold, used in a cell like: =AGELIST(A1:B10, 10, "UNDER")
Dim cell As Range, Buf As String
If MyRNG.Columns.Count <> 2 Then
AGELIST = "1: Use a two-column range"
Exit Function
ElseIf Not Age > 0 Then
AGELIST = "2: Use a whole number for the age"
Exit Function
ElseIf InStr(1, "OVERUNDER", ListDir) = 0 Then
AGELIST = "3: OVER and UNDER only"
Exit Function
End If
Select Case ListDir
Case "OVER"
For Each cell In MyRNG
If IsNumeric(cell) And cell >= Age Then Buf = Buf & ", " & cell.Offset(, -1)
Next cell
Case "UNDER"
For Each cell In MyRNG
If IsNumeric(cell) And cell <= Age Then Buf = Buf & ", " & cell.Offset(, -1)
Next cell
End Select
If Len(Buf) = 0 Then
AGELIST = "none found"
Else
AGELIST = "Kids aged " & Age & " and " & ListDir & " are:" & Mid(Buf, 3, Len(Buf))
End If
End Function
Used in a cell like:
=AGELIST(A1:B10, 10, "OVER")
The first parameter is your two-column source range
The second parameter is the age threshold
The third parameter is OVER or UNDER, sets the direction the threshold works
Bookmarks