How can I count the unique values within a specified range using VBA? 2

How can I count the unique values within a specified range using VBA?

This formal description explains the process of counting unique values within a specified range using VBA. VBA, or Visual Basic for Applications, is a programming language used to automate tasks in Microsoft Office applications, including Excel. To count unique values within a specified range in Excel using VBA, the following steps must be followed:
1. Select the range of data in which the unique values are to be counted.
2. Create a new module in the Visual Basic Editor.
3. Use the “For Each” loop to iterate through each cell in the selected range.
4. Use the “WorksheetFunction.CountIfs” method to check if the value in each cell is unique within the specified range.
5. If the value is unique, add it to a separate list.
6. Finally, use the “WorksheetFunction.CountA” method to count the number of unique values in the list.
By following these steps, one can efficiently count the unique values within a specified range using VBA.

Count Unique Values in Range Using VBA


You can use the following basic syntax to count the number of unique values in a range using VBA:

Sub CountUnique()
    Dim Rng As Range, List As Object, UniqueCount As Long
    Set List = CreateObject("Scripting.Dictionary")
    
    'count unique values in range A2:A11ForEach Rng In Range("A2:A11")
      If Not List.Exists(Rng.Value) Then List.Add Rng.Value, Nothing
    Next    'store unique count
    UniqueCount = List.Count
    
    'display unique count
    MsgBox "Count of Unique Values: " & UniqueCount

End Sub

This particular example counts the number of unique values in the range A2:A11 and then displays the count in a message box.

The following example shows how to use this syntax in practice.

Example: Count Unique Values in Range Using VBA

Suppose we have the following list of basketball team names in Excel:

Suppose we would like to count the number of unique team names in the range A2:A11.

We can create the following macro to do so:

Sub CountUnique()    Dim Rng As Range, List As Object, UniqueCount As Long
    Set List = CreateObject("Scripting.Dictionary")'count unique values in range A2:A11    For Each Rng In Range("A2:A11")
      If Not List.Exists(Rng.Value) Then List.Add Rng.Value, Nothing
    Next'store unique count
    UniqueCount = List.Count
    
    'display unique count
    MsgBox "Count of Unique Values: " & UniqueCount

End Sub

When we run this macro, we receive the following output:

VBA count unique

The message box tells us that there are 5 unique team names.

We can verify that this is correct by manually identifying each of the unique team names:

  • Mavs
  • Heat
  • Nets
  • Warriors
  • Kings

There are indeed 5 unique team names.

Note: To count the number of unique values in a different range, simply change A2:A11 in the For Each loop to a different range.

Cite this article

stats writer (2024). How can I count the unique values within a specified range using VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-count-the-unique-values-within-a-specified-range-using-vba/

stats writer. "How can I count the unique values within a specified range using VBA?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-count-the-unique-values-within-a-specified-range-using-vba/.

stats writer. "How can I count the unique values within a specified range using VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-count-the-unique-values-within-a-specified-range-using-vba/.

stats writer (2024) 'How can I count the unique values within a specified range using VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-count-the-unique-values-within-a-specified-range-using-vba/.

[1] stats writer, "How can I count the unique values within a specified range using VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I count the unique values within a specified range using VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top