SOLVED

Need VBA to auto-sum two fields

%3CLINGO-SUB%20id%3D%22lingo-sub-2538308%22%20slang%3D%22en-US%22%3ENeed%20VBA%20to%20auto-sum%20two%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2538308%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20fairly%20confident%20in%20my%20ability%20to%20edit%20VBA%2C%20but%20starting%20from%20scratch%20is%20a%20little%20more%20challenging.%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20file%20with%20production%20numbers%2C%20sorted%20by%20product%20code.%20The%20nature%20of%20my%20business%20is%20that%20some%20partial%20pallets%20get%20held%20to%20the%20next%20day%20in%20order%20to%20fill%20them%20to%20the%20top%20with%20product.%20So%2C%20for%20daily%20yields%2C%20I%20need%20to%20take%20out%20the%20previous%20day's%20production%20from%20today's%20numbers.%20So%2C%20my%20file%20contains%20code%20%221234%22%20with%20%22-1%22%20pallet%20and%20%22-500%22%20lbs.%20and%20a%20second%20line%20(today's%20totals)%20with%20code%20%221234%22%2C%20%2215%22%20pallets%20and%20%2215000%22%20lbs.%20I%20want%20the%20macro%20to%20search%20for%20duplicate%20product%20codes%2C%20combine%20the%20two%20pallet%20and%20lb.%20numbers%20and%20then%20delete%20the%20line%20with%20the%20negative%20amounts.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20the%20result%20of%20the%20below%20lines%20would%20be%20a%20%3CU%3Esingle%20line%3C%2FU%3E%20showing%2014%20pallets%20and%2014500%20lbs.%3C%2FP%3E%3CP%3EA%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BB%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20C%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20D%3C%2FP%3E%3CP%3E1234%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20description%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20-1%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B-500%3C%2FP%3E%3CP%3E1234%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20description%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2015%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B15000%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eof%20course%2C%20this%20would%20need%20to%20loop%20until%20no%20more%20duplicates%20are%20found.%20And%2C%20to%20add%20a%20snag%2C%20or%20error-handling%2C%20if%20there%20are%20no%20duplicates%2C%20but%20still%20have%20negatives%20in%20either%20pallet%20or%20weight%20field%2C%20it%20should%20ignore%20those%20(and%20I'll%20deal%20with%20them%20manually).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%20for%20any%20help%20you%20can%20offer.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2538308%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2538547%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20VBA%20to%20auto-sum%20two%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2538547%22%20slang%3D%22en-US%22%3EWhy%20do%20you%20not%20use%20a%20Pivottable%20or%20normal%20Grouping%20or%20PowerQuery%20to%20Group%20and%20get%20a%20new%20Table.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2538571%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20VBA%20to%20auto-sum%20two%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2538571%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1068158%22%20target%3D%22_blank%22%3E%40German_Chris%3C%2FA%3E%26nbsp%3BThanks%20for%20the%20reply.%3C%2FP%3E%3CP%3EI%20need%20to%20keep%20the%20formatting%20of%20the%20original%20in%20order%20to%20copy%2Fpaste%20into%20our%20P%26amp%3BL%20spreadsheet.%20So%2C%20a%20pivot%20table%20wouldn't%20necessarily%20be%20the%20best%20solution%20(I%20have%20other%20fields%20than%20just%20the%203%20mentioned%20that%20need%20to%20transfer%20over%20as%20well).%20A%20pivot%20table%2C%20even%20though%20a%20macro%20to%20create%20it%20would%20make%20it%20faster%2C%20is%20probably%20not%20worth%20the%20hassle.%20I%20don't%20think%20I've%20ever%20used%20a%20Power%20Query%2C%20so%20I'm%20not%20sure%20if%20that%20would%20work%20or%20not.%26nbsp%3B%3C%2FP%3E%3CP%3EIt's%20a%20fairly%20simple%20text%20file%20that%20I%20download%20from%20our%20SOR%20and%20then%20manipulate%20(as%20explained)%20after%20some%20research.%20Then%20I%20copy%20the%20info%20to%20a%20shared%20workbook%20where%20it%20does%20a%20bunch%20of%20other%20calculations.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2538574%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20VBA%20to%20auto-sum%20two%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2538574%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1101340%22%20target%3D%22_blank%22%3E%40RandomPanda%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAssuming%20that%20the%20data%20are%20in%20columns%20A%20to%20D%20and%20have%20headers%20in%20row%201%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20Combine()%0A%20%20%20%20Dim%20r%20As%20Long%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20m%20%3D%20Range(%22A%22%20%26amp%3B%20Rows.Count).End(xlUp).Row%0A%20%20%20%20Range(%22A1%22).CurrentRegion.Sort%20Key1%3A%3DRange(%22A1%22)%2C%20Header%3A%3DxlYes%0A%20%20%20%20For%20r%20%3D%20m%20-%201%20To%202%20Step%20-1%0A%20%20%20%20%20%20%20%20If%20Range(%22A%22%20%26amp%3B%20r%20%2B%201).Value%20%3D%20Range(%22A%22%20%26amp%3B%20r).Value%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Range(%22C%22%20%26amp%3B%20r).Value%20%3D%20Range(%22C%22%20%26amp%3B%20r).Value%20%2B%20Range(%22C%22%20%26amp%3B%20r%20%2B%201).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20Range(%22D%22%20%26amp%3B%20r).Value%20%3D%20Range(%22D%22%20%26amp%3B%20r).Value%20%2B%20Range(%22D%22%20%26amp%3B%20r%20%2B%201).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20Range(%22A%22%20%26amp%3B%20r%20%2B%201).EntireRow.Delete%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20r%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIf%20you%20don't%20have%20headers%20in%20row%201%2C%20use%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3E%20%20%20%20For%20r%20%3D%20m%20-%201%20To%201%20Step%20-1%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2539125%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20VBA%20to%20auto-sum%20two%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2539125%22%20slang%3D%22en-US%22%3EThank%20you%20so%20much%2C%20Hans.%20Even%20though%20this%20only%20takes%20a%20couple%20minutes%20off%20my%20daily%20routine.%20It%20will%20certainly%20help%20when%20I%20have%20a%20lot%20of%20these%20codes.%3C%2FLINGO-BODY%3E
Occasional Contributor

I'm fairly confident in my ability to edit VBA, but starting from scratch is a little more challenging. 

I have a file with production numbers, sorted by product code. The nature of my business is that some partial pallets get held to the next day in order to fill them to the top with product. So, for daily yields, I need to take out the previous day's production from today's numbers. So, my file contains code "1234" with "-1" pallet and "-500" lbs. and a second line (today's totals) with code "1234", "15" pallets and "15000" lbs. I want the macro to search for duplicate product codes, combine the two pallet and lb. numbers and then delete the line with the negative amounts. 

 

So the result of the below lines would be a single line showing 14 pallets and 14500 lbs.

A               B                    C                D

1234      description        -1             -500

1234      description        15             15000

 

of course, this would need to loop until no more duplicates are found. And, to add a snag, or error-handling, if there are no duplicates, but still have negatives in either pallet or weight field, it should ignore those (and I'll deal with them manually).

 

Thanks in advance for any help you can offer. 

 

6 Replies
Why do you not use a Pivottable or normal Grouping or PowerQuery to Group and get a new Table.

@German_Chris Thanks for the reply.

I need to keep the formatting of the original in order to copy/paste into our P&L spreadsheet. So, a pivot table wouldn't necessarily be the best solution (I have other fields than just the 3 mentioned that need to transfer over as well). A pivot table, even though a macro to create it would make it faster, is probably not worth the hassle. I don't think I've ever used a Power Query, so I'm not sure if that would work or not. 

It's a fairly simple text file that I download from our SOR and then manipulate (as explained) after some research. Then I copy the info to a shared workbook where it does a bunch of other calculations.

best response confirmed by RandomPanda (Occasional Contributor)
Solution

@RandomPanda 

Assuming that the data are in columns A to D and have headers in row 1:

Sub Combine()
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    m = Range("A" & Rows.Count).End(xlUp).Row
    Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Header:=xlYes
    For r = m - 1 To 2 Step -1
        If Range("A" & r + 1).Value = Range("A" & r).Value Then
            Range("C" & r).Value = Range("C" & r).Value + Range("C" & r + 1).Value
            Range("D" & r).Value = Range("D" & r).Value + Range("D" & r + 1).Value
            Range("A" & r + 1).EntireRow.Delete
        End If
    Next r
    Application.ScreenUpdating = True
End Sub

If you don't have headers in row 1, use

    For r = m - 1 To 1 Step -1
Thank you so much, Hans. Even though this only takes a couple minutes off my daily routine. It will certainly help when I have a lot of these codes.

@German_Chris You are sooooo right. Quite amazing to see that people still choose to revert to complicated VBA solutions to do very simple things, rather than using built-in functionalities that require a few button clicks. No coding required. Attaching a workbook that demonstrates just that.