Sum Unique/Distinct Values in Excel

Usually in the sequential database system, we can SUM only unique values in table column by adding all the values from SELECT DISTINCT query result only.

Based on the same principle, we can also create a simple VBA function in Microsoft Excel by adding the values only available in a collection of unique values we have created before hand.

Public Function DISTINCTSUM(Rg As range)
    Dim rCell As range
    Dim cCells As New Collection
    Dim vValue As Variant

    ' create a unique no duplicate value collection
    For Each rCell In Rg
        On Error Resume Next
        cCells.Add rCell.Value, CStr(rCell.Value)
    Next rCell

    ' sum all the data in previous collection
    For Each vValue In cCells
        DISTINCTSUM = vValue + DISTINCTSUM
    Next vValue

    Set cCells = Nothing
End Function

In here, first we create a Collection object that contains unique values only. Only then will we loop throught the collection and add up all the values stored there to get the DISTINCT SUM value.

Now we can call the above DISCTINCTSUM formula directly from Excel formula bar to sum only the unique or disctinct values in some range of cells, with the cell range as parameter.

=distinctsum(D4:D8)

Will sum all the unique values in range of D4 until D8. This is really helpful if we have a lot of duplicate data that we don’t want to count.

The other way around to get the sum of unique/distinct values in Excel is using Pivot Table.

Create a Pivot Table, then drop the column containing duplicate values in the Row fields area. Excel will then automatically filtered all the values and remove any duplicate values in that column. Later on, simply SUM all the data in the row fields to get the distinct sum.

The final result will be the same, but we don’t want to create a Pivot Table for every column we wanna sum, right?

Another way to get a list of unique values is to use the Advanced Filter feature. In advanced filtering options, check the option Copy to another location, and Unique records only option. Then select the cell where we want to put the unique values.

Easier to do compare to Pivot Table, but we still don’t have the flexibility of the previous DISTINCTSUM macro.

This entry was posted in Excel VBA Function. Bookmark the permalink.

14 Responses to Sum Unique/Distinct Values in Excel

  1. Tina Patterson says:

    i am trying to create a unique list from 3 specific columns… for example Reason 1, Reason 2, and Reason 3

    There will be duplicates in all columns because we are listing them in order of importance based on that particular item.

    I am trying to take those 3 columns, create a unique distinct list of “reasons” and for each unique item have a count of how many times that reason appears.

    I was trying to use your DISTINCTSUM macro… I am getting a unique list but no counts and wondered how I could get back a list with the counts associated with the items…

    For example if my data look like this

    Reason 1 Reason 2 Reason 3
    banana Apple Peach
    Peach grape banana
    lemon lime grape

    what I want back from the macro is
    Banana 2
    Apple 1
    Peach 2
    grape 2
    lemon 1
    lime 1

    Can you help?

    Thanks very much!

  2. ashish says:

    i want a formula in ms excel to store previous value and add new value in previous value. i want to change value in one cell only and display value with adition of previous value in another cell.

  3. Deepak Bhardwaj says:

    How to make macro in excel 2007 to get pivot value in new workbook. Please help

  4. Pingback: giant twist comfort cs

  5. Pingback: The Paleo Diet

  6. Pingback: Bail Bonds Los Angeles

  7. Pingback: Court Ordered Anger Management Classes

  8. Pingback: debt consolidation companies

  9. Pingback: reverse mortgage disadvantages

  10. Pingback: debt consolidation companies

  11. Pingback: credit card debt consolidation loan

  12. Pingback: debt help

  13. Pingback: air purifier for dust

  14. Pingback: Discount Golf Balls

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>