Merge/Combine Data, not sure of syntax

  • I am not sure exactly of the syntax I should be looking for in these forums. I am new to VB, so flame on, I dont mean to be lame.


    I want to take the data, as seen below, and combine all the like Bar MArk's, into one line, and add up the QTY, FT & IN columns. The Bar Size column will not be added up. I would also like to get rid of the zeros. I will be doing this hundreds of times a month, so any help is GREATLY appreciated.


    I have the following table:


    QTY BAR SIZE FT IN BAR MARK
    4 -- 3 10 RCBC1
    4 -- 3 10 RCBC1
    4 -- 3 10 RCBC1
    2 6 7 3 MK646
    2 6 7 3 MK646
    2 6 7 3 MK646
    3 6 4 5 MK642
    3 6 4 5 MK642
    3 6 4 5 MK642
    1 6 4 6 MK641
    1 6 4 6 MK641
    1 6 4 6 MK641
    8 6 2 1 MK631
    8 6 2 1 MK631
    8 6 2 1 MK631
    25 4 6 9 MK405
    25 4 6 9 MK405
    25 4 6 9 MK405
    46 4 2 6 MK404
    46 4 2 6 MK404
    46 4 2 6 MK404
    46 4 7 9 MK402
    46 4 7 9 MK402
    46 4 7 9 MK402
    4 11 49 2 114902
    4 11 49 2 114902
    4 11 49 2 114902
    2 8 14 3 81403
    2 8 14 3 81403
    2 8 14 3 81403
    6 8 5 2 80502
    6 8 5 2 80502
    6 8 5 2 80502
    16 4 2 9 40209
    16 4 2 9 40209
    16 4 2 9 40209
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0



    Here is an example of the outcome I would like:


    4 -- 3 10 RCBC1
    4 -- 3 10 RCBC1
    4 -- 3 10 RCBC1


    would turn into this:


    12 0 9 30 RCBC1


    And so on...and deleteing the ZEROS at the bottom..


    Is this possible?

  • Hi,


    in cell F2
    =IF(COUNTIF(E$2:E2,E2)=1,ROW(),"") and drag down to the last row of the data in Col.A


    in cell G2
    =IF($F2<>"",SUMIF($E:$E,$E2,A:A),"") and drag to the right up to cell J2


    in cell K2
    =IF($F2<>"",E2,"")


    then, select cells from G2 to K2 and drag down as long as you need.
    select entire columns from F to K --> copy --> pastescpecial/values
    sort by col.F in ascending order


    if you don't need origial data, just delete columns from A to F


    jindon

  • EDIT: I just saw that I misread your post. You were grouping based only on column E, I grouped based on the contents of the entire row matching. I need to fix my solution and post back.



    I see jindon has already posted a formula solution while I was composing, but for whatit is worth, here is a VBA solution. It sorts the data first to make sure that all identical rows are grouped together, so it changes the order of yoru sample data (which was, I think, already grouped). If your data is always grouped appropriately, you can remove the sorting code.



    The file with your sample data and the above macro is attached.

  • Updated (Fixed) Code


    Ok, here is some updated code that matches the problem you posted and and updated attachment.


  • Outstanding!!! Thank you all. I have made a mistake in my interpretation of what needed to be done. The BAR FT IN MARK need to be left alone, just combined, and the QTY is the only column that needs to be added up....I did not think I would have had so much to work with. I will try to look at the code and work with it to modify it. Thanks again. I REALLY appreciate it. If you have a simple modification fix, please dont hesitate. I am really new to this wonderful world of VB, and now can appreciate the fun that can be placed back into Excel.


    :thanx:

  • I have not had a chance to test this, but I think all you need to do is change the following portion of the code:


    Code
    ' Update variables if the next row is the same
            If MARK = Cells(r, 5).Value Then 
                QTY = Cells(r, 1).Value + QTY 
                If IsNumeric(Cells(r, 2)) Then 
                    BAR = Cells(r, 2).Value + BAR 
                Else: BAR = 0 
                End If 
                FT = Cells(r, 3).Value + FT 
                INCH = Cells(r, 4).Value + INCH 
                Rows(r).EntireRow.Delete


    to


    Code
    ' Update variables if the next row is the same
            If MARK = Cells(r, 5).Value Then 
                QTY = Cells(r, 1).Value + QTY 
                Rows(r).EntireRow.Delete


    This should increment QTY only.

  • Thats IT!!!


    Wow, that was a simple modification!!! That is exactly the outcome I needed! Thanks again. I am just so happy. I have been writing this code for 3 days now, and that was the last part I need to figure out. Now I can finish it up, and move on to learn more. I have been learning to write AutoCAD to Excel. This is great!


    :thanx:

  • 1st Item's QTY will not add up? Puzzled, can't determine why.


    I am done coding my entire app and checking values, and notice that the first line of the data does not add up the QTY, it combines it ok. I cant seem to figure it out.


    Just look at the file attached in the previous post for code and data: Group & Add v2.xls


    If you look at the original data:
    16 4 2 9 40209
    16 4 2 9 40209
    16 4 2 9 40209


    3 rows should add up to QTY=48, but sorted data reads 16 on DATA tab.


    I ran a few tests and it seems that the 1st sorted item is the only one not adding up. Any help is appreciated. Thanks! I'll keep workin gon it.

  • Work Around


    I found a work around by creating a bogus data line that would always be sorted first, that wat the QTY would count up right, then I just delete the bogus data at the end, but since I am learning, I was hoping somebody could explain why?


    Thanks everyone.

  • Oops


    Good catch. My code never got around to printing out the last set of accumulations. This also left the top row of the last data grouping untouched (never overwritten) and in place. -- A nice story line but a very weak ending!


    Here is code that should work (file attached). It assumes there is a header row and the data start in row 2.


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!