Hi all!
I'm trying to modify the following code so that it takes into account not only DATE but also a CODE when grouping entries.
Currently the code takes data in columns A-E and combines all entries that have the same date in one row and sums related data for each entry in each of the columns. This worked well but now we'd like to add a defining code to the scenario.
What I'd like the code to do is look to dates in column A and codes in column B and combine all entries with the same date and code into single rows on a new sheet. I have attached an example of what the data looks like and what I'd like to see as an output. People have told me this wouldn't be difficult but given my limited VBA experience I'm having some issues.
Here is the code I had been using (constructed with help from great users of this forum):
(There may very well be a better solution to what I'm trying to do! If so I'd love pointers in the right direction.)
newexample.xlsm
Option Explicit
Sub Combine()
Dim i As Long, ii As Long, j As Long, bot As Long, mbot As Long, rec As Long
ReDim MyGrid(4, 0) As Variant
Sheets("Sheet1").Select
bot = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To 5
mbot = Application.WorksheetFunction.Max(mbot, Cells(Rows.Count, i).End(xlUp).Row)
Next
For i = 2 To bot ' starts second row and runs to the calcualted bottom
j = i + 1
Do Until Cells(j, 1) <> "" Or j = mbot + 1
j = j + 1
Loop
j = j - 1
ReDim Preserve MyGrid(4, UBound(MyGrid, 2) + 1)
rec = UBound(MyGrid, 2)
MyGrid(0, rec) = Cells(i, 1)
For ii = i To j
MyGrid(1, rec) = MyGrid(1, rec) + Cells(ii, 2)
MyGrid(2, rec) = MyGrid(2, rec) + Cells(ii, 3)
MyGrid(3, rec) = MyGrid(3, rec) + Cells(ii, 4)
MyGrid(4, rec) = MyGrid(4, rec) + Cells(ii, 5)
Next
i = j
Next
Sheets("Sheet2").Select
Cells.Clear
bot = Cells(Rows.Count, 1).End(xlUp).Row
For i = 0 To rec
Cells(bot + 1, 1) = MyGrid(0, i)
Cells(bot + 1, 2) = MyGrid(1, i)
Cells(bot + 1, 3) = MyGrid(2, i)
Cells(bot + 1, 4) = MyGrid(3, i)
Cells(bot + 1, 5) = MyGrid(4, i)
bot = bot + 1
Next
End Sub
Bookmarks