Got any Excel/VBA Questions? Free Excel Help.
This UDF will count each entry in a Range, once.
The Code
Function Count_once(Count_range As Range) As Long Dim strAddress As String Dim lMaxRow As Long, lEndCol As Long, lStartCol As Long Dim lColCount As Long Dim lLoop As Long, lArrElement As Long Dim lArray() As Long lMaxRow = Rows.Count lColCount = Count_range.Columns.Count lEndCol = Count_range.Columns(lColCount).Column ReDim lArray(lColCount) lStartCol = Count_range.Columns(1).Column For lLoop = lStartCol To lEndCol lArray(lArrElement) = Cells(lMaxRow, lLoop).End(xlUp).Row lArrElement = lArrElement + 1 Next lLoop lMaxRow = WorksheetFunction.Max(lArray) strAddress = Range(Count_range.Cells(1, 1), _ Cells(lMaxRow, lEndCol)).Address Count_once = Evaluate("sumproduct((" & strAddress & "<>"""")/" _ & "countif(" & strAddress & "," & strAddress & "&""""))")-1 End Function
To use this UDF push Alt+F11 and go Insert>Module and paste in the code. Push Alt+Q and save. The Function will appear under "User Defined" in the Paste Function dialog box (Shift+F3). Use the Function in any cell as shown below.
=Count_once(A1:A10)
See also:
Count Or Sum Specified Number In a Single Cell |
VBA Count of Each Item in a List |
Custom Excel Function (UDF) that will Count Words in Excel |
Free Training Course: Lesson 1 - Excel Fundamentals
See also: Index to Excel VBA Code; Index to Excel Freebies; Lesson 1 - Excel Fundamentals; Index to how to… providing a range of solutions
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.