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 FunctionIn 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.
Related Entries
Auto Expanding VLOOKUP Table Array
Ok, this time I want to talk about how to create a vlookup table array that automatically expanding in accordance with the numbers of available data. As we know, the basic form of Excel VLOOKUP to look for A1 value in range B1:C15 column no 2 is like this: =VLOOKUP(A1,B1:C15,2) With parameters such as the value we looking...
Excel VBA Add Set of Worksheets Automatically
There is a time when we must create a set of Excel Worksheets templates on a regular basis in our work. For example, a set of Excel Worksheet for each month of the year or may be based on the type of work we should done in certain time base. Here a simple example of how...
Get Position of Last Column Containing Data with Excel VBA
If you read my latest update on how we can get position of last row containing data, then most probably you already have a glimpse on how we perform the same task with last column containing data in Excel. The VBA code is the exact replica of the code to get the last row, only...
Check If Excel Workbook is Already Open or Not
In my case, I work a lot with transferring data from current Excel Workbook to another Excel Workbook, and to be able to do that, of course I need to make sure whether the destination Workbook is already open or not. The following excel vba function assigned to check whether a workbook we need is open...
Move Cursor To One Cell Below Last Row With Data
Ever want to paste some data into new empty cell after the last cell with data in certain column? Then take a look at this simple macro.'first, select cell in the first row of that column, like A1, K1, etc Range("A1").Select'move to the last...
Valuable Resources
- Microsoft Excel 2003/2007 Video Tutorials
Step-by-step video guide to mastering Charts, PivotTable, Data Analysis and Macro programming in Microsoft Excel in 5 hours.
- 101 Secrets of Microsoft Excel
Discover 101 of Excels little-known secrets that have been hiding right under your nose.
- Top Tips & Tricks to Get You Started in Microsoft Excel 2007
This is an ebook by Carol Bratt's, a Microsoft Certified Professional who has plenty of experience explaining things in simple English.
- Microsoft Office 2003/XP/2007 Training Videos
Discover an easy to use, hands-on interactive course that will teach you how to unleash the true power of Microsoft Office 2003/XP/2007.
If you found this page useful, please consider bookmark it using social media or add a link to this page.
Incoming search engine terms: vba sum certain cells in a column, How to add numbers with excel macros, excel vba uniques column, how to find field values generated by formula using excel macro, How to find the numbers that make up sum in excel, how to get a count of unique entries excel 2010, How to get sum of rows from filtered result in other column, how to get the sum in excel, how to make all pivot table fields sum vba, how to select only certain rows with certain value in excel?, how to sum duplicate numbers in excel, how to sum same values of duplicate records in Excel 2007, how we use unique value in excel sheet, if then excel add to sum, is there any way to display all duplicate cells in a pivot table