I am extending the closest value technique I published recently to calculate the same based on a filtered list.
In this demonstration, the goal is to highlight values in a numeric range that are clostest to the average of that range in a filtered list. So, we first make the range dynamic with the following defined name formula.
Then, we modify that range to include only filtered values.
For more information on the SUBTOATL function as used here, see:
Next, we use that range to make an array of the absolute differences of each value of the range from the average.
We can then define a cell for the number of values to highlight.
The heavy work is done by the next formula, which creates an array of the N values to be higlighted.
This formula returns the position of each smallest deviation in the 2nd argument of the INDEX function, which then returns the values corresponding to those deviations, based on a filtered list. The use of the formula syntax needed to do this with the INDEX function is explained at the following link.
This formula can now be used in the creation of the CF, where CF Formula is =SUM(N(A1=Num_Array)), starting at A1 and applied to all of Column A.
The result of this CF is shown below.
The example file can be downloaded here.