VBA for combining duplicate columns and sum the value in different cells

darrenliux

New Member
Joined
May 13, 2018
Messages
15
Hi
I'm looking for a VBA code to solve the problem below, the input will start in Cell I2. I'd really appreciate any help on this.
Raw input table:
ABCDEFGHIJKLMNOPQR
1
220002001200220032000200120022003... to continue up to 30 columns
312349999
4
5
6

<tbody>
</tbody>

to consolidate data to (Solution is under the original data)
ABCDEFGHIJKLMNOPQR
1
220002001200220032000200120022003...to continue up to 30 columns
312349999
42000200120022003
510111213
6

<tbody>
</tbody>


Thank you in advance for any help
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Code:
Sub t()
    With ActiveSheet
        For i = 9 To 12
            .Cells(4, i) = .Cells(2, i).Value
            .Cells(5, i) = Application.SumIf(.Cells(2, i).Resize(, 30), .Cells(2, i).Value, .Cells(3, i).Resize(, 30))
        Next
    End With
End Sub
 
Upvote 0
Code:
Sub t()
    With ActiveSheet
        For i = 9 To 12
            .Cells(4, i) = .Cells(2, i).Value
            .Cells(5, i) = Application.SumIf(.Cells(2, i).Resize(, 30), .Cells(2, i).Value, .Cells(3, i).Resize(, 30))
        Next
    End With
End Sub

Hi JLGWhiz,
Thank you very much for your code.
I've just tried it. The first 4 years worked perfectly.
However, because there are more than 4 different years in my data, the rest of the years don't appear. (please see below)
Could you please have a look and update it if possible. Many thanks.

200520062008201820162018200520062008201020102006
5172896562710
2005200620082018
11161311

<colgroup><col><col><col span="2"><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
Two questions
1. How many unique years do you have on the row?

2. Do they run consescutively before starting to repeat, or are they randomly distributed?
 
Upvote 0
Disregard post #4 . Try this version.

Code:
Sub t2()
Dim lr As Long, c As Range, col As Long
lr = ActiveSheet.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
With ActiveSheet
    .Range("I2", .Cells(2, Columns.Count).End(xlToLeft)).Copy
    .Cells(lr + 2, 1).PasteSpecial xlPasteValues, Transpose:=True
    .Cells(lr + 2, 1).CurrentRegion.AdvancedFilter xlFilterCopy, , .Cells(lr + 2, 3), True
    .Cells(lr + 2, 1).CurrentRegion.ClearContents
    .Cells(lr + 2, 3).CurrentRegion.Sort .Cells(lr + 2, 3), xlAscending
    col = 9
    For Each c In .Cells(lr + 2, 2).CurrentRegion.Offset(1)
        If c <> "" Then
            .Cells(4, col) = c.Value
            .Cells(5, col) = Application.SumIf(.Cells(2, col).Resize(, 30), c.Value, .Cells(3, col).Resize(, 30))
            col = col + 1
        End If
    Next
    .Cells(lr + 2, 2).CurrentRegion.ClearContents
End With
End Sub
 
Last edited:
Upvote 0
Hi JLGWhiz
Thank you again for the code.
There are still a bit error when I tried it.
Year 1989 is missing, and it returns 0 for some of the years.
2001200620182005200320092001200720172015201519892003200619892003
223653987121461111
2001200120032005200620072009201520172018
119761802670

<colgroup><col><col span="2"><col span="3"><col span="3"><col span="2"><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
Hi JLGWhiz
can you please amend the code, so that the solution overwrite the original data instead of below it.
Thank you.
 
Upvote 0
I believe this fixes the skipped years and zero values. It does not overwrite the original values, but instead deletes the original values on rows 2 and 3 and shifts the new values up. If it had overwritten the original values, then you would have had some of the old values tagged onto the end of the new values, since there were more years entered for the original values.

Code:
Sub t3()
Dim lr As Long, lc As Long, c As Range, col As Long
lc = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
lr = ActiveSheet.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
With ActiveSheet
    .Range("I2", .Cells(2, Columns.Count).End(xlToLeft)).Copy
    .Cells(lr + 2, 1).PasteSpecial xlPasteValues, Transpose:=True
    Application.CutCopyMode = False
    .Cells(lr + 2, 1).CurrentRegion.Sort .Cells(lr + 2, 1), xlAscending
    .Cells(lr + 2, 1).CurrentRegion.AdvancedFilter xlFilterCopy, , .Cells(lr + 2, 3), True
    .Cells(lr + 2, 1).CurrentRegion.ClearContents
    col = 9
    For Each c In .Cells(lr + 2, 3).CurrentRegion.Offset(1)
        If c <> "" Then
            .Cells(4, col) = c.Value
            .Cells(5, col) = Application.SumIf(.Range(.Cells(2, 9), .Cells(2, lc)), c.Value, .Range(.Cells(3, 9), .Cells(3, lc)))
            col = col + 1
        End If
    Next
    .Range(.Cells(2, 9), .Cells(3, lc)).Delete xlShiftUp
    .Cells(lr + 2, 3).CurrentRegion.ClearContents
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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