VBA: User Defined Function that Counts Selected Cells

Automate Excel

VBA: User Defined Function that Counts Selected Cells

The following function will examine a range of Excel cells and return the number that fall within a specified range:

Function Count_Selected_Cells(Data_Range As Range, Min As Double, Max As Double) As Integer
Dim Cell
Count_Selected_Cells = 0#
For Each Cell In Data_Range
    If ((Cell.Value >= Min) And (Cell.Value <= Max)) Then
        Count_Selected_Cells = Count_Selected_Cells + 1
    End If

Next Cell
End Function

It can be used as:

=Count_Selected_Cells(B5:F14,12,50)

Will return the number of cells having values between 12 and 50 in the range B5:F14:

To download the .XLSM file from the article, click here

Related posts

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.