Consulting

Results 1 to 6 of 6

Thread: Get unique items and sum total on userform

  1. #1

    Get unique items and sum total on userform

    Hello everyone
    I have a textbox and listbox on userform and I need to populate the data in range A2:D17 but in specific way
    As for each item would be populated once and of course to get the total of quantities and total of prices for that item
    For example : "Item1" would be in listbox like that
    Item1 7 200

    7 is the total of quantities and 200 total of prices
    Thanks advanced for help
    Attached Files Attached Files
    Last edited by SamT; 03-03-2017 at 05:01 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    A to D is 4 columns. You only have three columns in the ListBox

    Something like:
    Private Sub CommandButton1_Click()
    With Me.ListBox1
    Sheet1.Range("A2").Resize(.ListCount, .ColumnCount) = .List
    End With
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Jan 2011
    Posts
    35
    Location
    Try this:-
    Private Sub UserForm_Initialize()
    Dim Rng As Range, Dn As Range, Q As Variant, Dic As Object
    Set Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
    Set Dic = CreateObject("scripting.dictionary")
    Dic.CompareMode = vbTextCompare
    For Each Dn In Rng
        If Not Dic.Exists(Dn.Value) Then
            Dic.Add Dn.Value, Array(Dn.Value, Dn.Offset(, 1).Value, Dn.Offset(, 2).Value)
        Else
            Q = Dic(Dn.Value)
                Q(1) = Q(1) + Dn.Offset(, 1).Value
                Q(2) = Q(2) + Dn.Offset(, 2).Value
            Dic(Dn.Value) = Q
        End If
    Next
    With Me.ListBox1
        .ColumnCount = 3
        .List = Application.Index(Dic.items, 0, 0)
    End With
    End Sub

  4. #4
    Thanks a lot Mr. Sam
    Thank you very much Mr. MickG. That's exactly what I was searching for
    You're awesome

  5. #5
    VBAX Regular
    Joined
    Jan 2011
    Posts
    35
    Location
    You're welcome

  6. #6
    I have studied this wonderful code and nearly I got most of it except the line of .List

    Can you explain this line for me please?
    .List = Application.Index(Dic.items, 0, 0)

Posting Permissions

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