0

I have the following type of table

Current table                                                   
P.no.   P.Line  ian feb mar apr mai iun iul aug sept    oct nov dec
100001  Line1   100                                         
100001  Line1                   100                         
100001  Line1                               300             
100001  Line2       200                                     
100002  Line2               100                             
100003  Line3   100                                         
100003  Line3       100                                     
100003  Line3           100                                 
100003  Line3                           300                 
100003  Line3                                   200         
100003  Line3                                               100

I need to make it look like this:

Needed result                                                   
P. no.  P.line  ian feb mar apr mai iun iul aug sept    oct nov dec
100001  Line1   100             100         300             
100001  Line2       200                                     
100002  Line2               100                             
100003  Line3   100 100 100             300     200         100

Thank you in advance.

4
  • Do you want a VBA solution or a worksheet function?
    – Joost
    Jan 17, 2019 at 21:45
  • VBA would fit me better, because I don't want the person that is usong the file to alter the formulas inside. Jan 18, 2019 at 6:52
  • How about using Power Query? Power Query can also analysis large data in Excel.
    – Lee
    Jan 18, 2019 at 9:11
  • I need to make it in VBA, because I will not be the one using the macro, so the person who uses it only to run the macro and generate the table exactly as I want. The macro will be used on multiple instances of this file(every day another is generated by the system), with the same columns template. Jan 18, 2019 at 12:24

2 Answers 2

0

You can create a separate summary table and use SUMIFS. I created a table outline that looked like this:

P.no.   P.Line  ian feb mar apr mai iun iul aug sept    oct nov dec
100001 Line 1
100001 Line 2
100002 Line 2
100003 Line 3

I did this by copying A & B and using Excel's remove duplicates. This formula can be used to sum for each column (assuming your data starts in column A and ian is in C):

=SUMIFS(C$2:C$12, $A$2:$A$12, $A15, $B$2:$B$12, $B15)

This will sum column C where column A is the same as A15 and column B is the same as B15. You can drag this formula right and down and it should be dynamic.

In your example you've returned blank if there's no data, this can also be done but you'll need to wrap the above into something like this:

=IF(SUMIFS(C$2:C$12, $A$2:$A$12, $A15, $B$2:$B$12, $B15) = 0, "", SUMIFS(C$2:C$12, $A$2:$A$12, $A15, $B$2:$B$12, $B15)) 

This just means if it sums to 0 then return blank, otherwise give me the sum.

screenshot

1
  • Thank you for the answear, but in I have a larger file, with 7 production lines, and around 300 product codes, VBA code would fit me better is you know how to. Jan 18, 2019 at 6:54
0

With s as your source range and t as your target range, I would do something like this:

' loop through the rows
for i=1 to s.rows.count
    ' check if the sum code is changed
    if s(i,1) & s(i,2) <> prev_id then trow = trow +1
    ' fill the target row with the source values
    for j=1 to s.columns.count
        if not isempty(s(i,j)) then t(trow, j) = s(i,j)
    next
    ' save the sum code (for the next loop)
    prev_id = s(i,1) & s(i,2)
next

Revision: Okay, based on your link I made this:

Sub CombineRows()
    Dim rw As Long
    Dim col As Long
    Dim rng As Range

    ' make sure the cursor is somewhere in the left columns
    Set rng = Selection.CurrentRegion
    For rw = rng.Rows.Count To 2 Step -1
        If rng(rw, 2) = rng(rw - 1, 2) And rng(rw, 3) = rng(rw - 1, 3) Then
            For col = 4 To rng.Columns.Count
                If Not IsEmpty(rng(rw, col).Value2) Then
                    rng(rw - 1, col).Value2 = rng(rw, col).Value2
                End If
            Next
            rng.Rows(rw).EntireRow.Delete
        End If
    Next
End Sub

Just stand somewhere in the left three columns and run the macro.

5
  • Sorry for late reply. So this is how the table actually looks. I sorted the lines by P. line and after by Material N. . I want to combine in one row the rows (each row is per 1 week) that have the same Material N. but are produced in different days. For the next month the columns name will remain the same, but rows data will differ. Jan 22, 2019 at 11:42
  • Document link: Combine_rows Jan 22, 2019 at 11:53
  • @PaulMihaiIrimescu What have you tried yourself? Can you share the code?
    – Joost
    Jan 22, 2019 at 14:53
  • I used something similar to this post link, but how do I make it for 31 columns? not only one. I am a beginner in VBA coding and don't know all the available combinations, that's why I'm asking for help. Jan 23, 2019 at 7:14
  • Could you let know if you have any difficulties? And if not, mark it as a solution?
    – Joost
    Jan 27, 2019 at 7:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .