Consulting

Results 1 to 10 of 10

Thread: Solved: Sum unique values in a range

  1. #1

    Solved: Sum unique values in a range

    Hi All,

    I need some help with to sum unique values in a range based on another cell value.

    I have Name in Column Name and Billed Amount in Col B:

    I need the Sum of unique values in col B. For e.g in attached sheet

    COl A has Names="Mango", COl B has Billed Amounts. Few of which are repetitive. I need such repetititves to get summed only once. I couldn't use a pivot. Can someone suggest some way around for this?
    Attached Files Attached Files

  2. #2
    VBAX Regular
    Joined
    Nov 2011
    Posts
    33
    Location
    Try to use this formula
    =DSUM(A1:B9,B1,A1:A2)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    with SUMIF

    [vba]=SUMIF(A2:A9,"Mango",B2:B9)[/vba]

    or
    [vba]=SUMIF(A2:A9,H1,B2:B9)[/vba]
    where H1 value is Mango.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]' =SUM(uniquevalues(B2:B9))
    Public Function UniqueValues(theRange As Range) As Variant
    Dim colUniques As New VBA.Collection
    Dim vArr As Variant
    Dim vCell As Variant
    Dim vLcell As Variant
    Dim oRng As Excel.Range
    Dim i As Long
    Dim vUnique As Variant
    Set oRng = Intersect(theRange, theRange.Parent.UsedRange)
    vArr = oRng
    On Error Resume Next
    For Each vCell In vArr
    If vCell <> vLcell Then
    If Len(CStr(vCell)) > 0 Then
    colUniques.Add vCell, CStr(vCell)
    End If
    End If
    vLcell = vCell
    Next vCell
    On Error GoTo 0

    ReDim vUnique(1 To colUniques.Count)
    For i = LBound(vUnique) To UBound(vUnique)
    vUnique(i) = colUniques(i)
    Next i

    UniqueValues = vUnique
    End Function

    [/VBA]

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [VBA]
    Sub M_snb()
    sn = Sheets("fruits").Cells(1).CurrentRegion

    With CreateObject("scripting.dictionary")
    For j = 2 To UBound(sn)
    .Item(sn(j, 1)) = .Item(sn(j, 1)) + sn(j, 2)
    Next

    Sheets("fruits").Cells(20, 1).Resize(.Count) = Application.Transpose(.keys)
    Sheets("fruits").Cells(20, 2).Resize(.Count) = Application.Transpose(.items)
    End With
    End Sub
    [/VBA]

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Of course there are several ways to do it. Like the dictionary method, collections can be used in a similar way somewhat. The function goes in a Module and the first line is an example use as a UDF in a cell.

    [VBA]' =SUM(UniqueValues(B2:B9))
    Public Function UniqueValues(theRange As Range) As Variant
    Dim colUniques As New VBA.Collection
    Dim vArr As Variant
    Dim vCell As Variant
    Dim vLcell As Variant
    Dim oRng As Excel.Range
    Dim i As Long
    Dim vUnique As Variant
    Set oRng = Intersect(theRange, theRange.Parent.UsedRange)
    vArr = oRng
    On Error Resume Next
    For Each vCell In vArr
    If vCell <> vLcell Then
    If Len(CStr(vCell)) > 0 Then
    colUniques.Add vCell, CStr(vCell)
    End If
    End If
    vLcell = vCell
    Next vCell
    On Error GoTo 0
    'MsgBox colUniques.Count
    ReDim vUnique(1 To colUniques.Count)
    For i = LBound(vUnique) To UBound(vUnique)
    vUnique(i) = colUniques(i)
    Next i

    UniqueValues = vUnique
    End Function[/VBA]

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Probably more robust:

    [VBA]Sub M_snb()
    sn = Sheets(1).Cells(1).CurrentRegion

    With CreateObject("scripting.dictionary")
    For j = 2 To UBound(sn)
    .Item(sn(j, 1)) = .Item(sn(j, 1)) + sn(j, 2)
    Next

    For Each it In .keys
    .Item(it) = Array(it, .Item(it))
    Next

    Sheets(1).Cells(20, 1).Resize(.Count, 2) = Application.Index(.items, 0, 0)
    End With
    End Sub[/VBA]

  8. #8
    Quote Originally Posted by snb
    Probably more robust:

    [vba]Sub M_snb()
    sn = Sheets(1).Cells(1).CurrentRegion

    With CreateObject("scripting.dictionary")
    For j = 2 To UBound(sn)
    .Item(sn(j, 1)) = .Item(sn(j, 1)) + sn(j, 2)
    Next

    For Each it In .keys
    .Item(it) = Array(it, .Item(it))
    Next

    Sheets(1).Cells(20, 1).Resize(.Count, 2) = Application.Index(.items, 0, 0)
    End With
    End Sub[/vba]

    snb

    None of your code is not complied with OP requirement
    "Sum unique values ​​in the range"

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    In that case:

    [vba]
    Sub M_snb()
    With ActiveSheet.ListObjects.Add(1, Range("$A$1:$B$9"), , 1)
    .Name = "snb_001"
    .Range.RemoveDuplicates Array(1, 2), 1
    .ShowTotals = True
    End With
    End Sub
    [/vba]

  10. #10
    Hi Snb, Kenneth, lotuxel, mancubus...

    Thanks for your help.. I got a handful of solutions now coz of you guys.

    Thanks a ton...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •