Identify records and add duplicates. (Consolidate)

hpernaf

New Member
Joined
Jul 1, 2019
Messages
27
Hi everyone!
I have a spreadsheet with product registration with the columns: ID, Product, Amount, Price, Total and Product type
1.PNG

These products are loaded into a sales registration form.
In this form I select the category: "Product type" and in the listbox the respective products of that category are loaded.
2.PNG


When I click the "Register Sale" button, the ListBox records are saved in the "Sales" sheet.
3.PNG


The problem is that I have repeated products in each category:
4.PNG

I would like to register only unique products. If I am making a duplicate record, I should just add the quantities.

That is, when registering the sale, I need to check if there are any items that have already been entered in the "Sales" spreadsheet. If there are repeated products, I just need to add the quantities: Sold Amount and Total $.

In this example, it would look like this:
5.png


It is possible? If anyone can help me, I will be very grateful.

Below is the spreadsheet I created.
 

Attachments

  • 1.PNG
    1.PNG
    16.3 KB · Views: 1
  • 2.PNG
    2.PNG
    13.7 KB · Views: 3
  • 3.PNG
    3.PNG
    11.5 KB · Views: 1
  • 4.PNG
    4.PNG
    14 KB · Views: 1
  • 5.PNG
    5.PNG
    17.2 KB · Views: 1

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Your link takes us to the Google Drive sign-in page. We need a direct link to your file.
 
Upvote 0
Replace your current macro with this one:
VBA Code:
Private Sub ButtonRegisterSale_Click()
    Application.ScreenUpdating = False
    Dim Rng As Range, RngList As Object, WS1 As Worksheet, WS2 As Worksheet, desWS As Worksheet, key As Variant, n, i As Integer
    Dim lastRow As Long, fVisRow As Long, lVisRow As Long, ID As Range, totC As Double, totE As Double, rowCount As Long, x As Long
    Set ws = ThisWorkbook.Sheets("Sales")
    ws.Activate
    n = ws.Range("A1").CurrentRegion.Rows.Count + 1
    i = ListBox1.ListCount - 1
    Range(ws.Cells(n, 1), Cells(n + i, 5)).Value = ListBox1.List
    lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each Rng In Range("A2", Range("A" & Rows.Count).End(xlUp))
        If Not RngList.Exists(Rng.Value) Then
            RngList.Add Rng.Value, Nothing
        End If
    Next
    For Each key In RngList
        With ActiveSheet
            .Cells(1, 1).CurrentRegion.AutoFilter 1, key
            rowCount = .[subtotal(103,A:A)] - 1
            If rowCount > 1 Then
                fVisRow = .Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
                lVisRow = .Cells(Rows.Count, "A").End(xlUp).Row
                
                For Each Rng In .Range("C" & fVisRow & ":C" & lVisRow).SpecialCells(xlCellTypeVisible)
                    totC = totC + Rng
                    totE = totE + Rng.Offset(, 2)
                Next Rng
                .Range("C" & fVisRow) = totC
                .Range("E" & fVisRow) = totE
                For x = lastRow To fVisRow + 1 Step -1
                    If .Rows(x).Hidden = False Then .Rows(x).Delete
                Next x
            End If
        End With
        totC = 0
        totE = 0
    Next key
    Range("A1").AutoFilter
    Unload Me
    Application.ScreenUpdating = True
    MsgBox "Sale successfully registered!"
End Sub
 
Upvote 0
"In the code you gave me, the data starts to be inserted in the spreadsheet from cell A2. How do I get them to start being inserted in cell A14? "

Could you please upload a version of the file that shows the expected results?
 
Upvote 0
I'm sorry but when I try to run the macro, I get errors in Private Sub UserForm_Initialize().
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top