VBA to sum values in one column based on criteria of another column

Jim885

Well-known Member
Joined
Jul 8, 2012
Messages
663
I need someone to help me with this, as I can't figure out how to do it.

The below sample is an accurate example of a much larger set of data that I need help with. Column H will be my desired result.
I need a macro that will look at the matching values (numerical value of month) of Column F, then enter the sum of the corresponding values (rows) contained in Column D for that month. The sum of each month should be placed in Column H at the last value of the row for each particular month.

Thanks to everyone for looking at this.

ABCDEFGH
1Item# DescriptionQuantity MonthYear
21001 Item 1
3
4DateTransaction Description
55/21/2013TX 8.00 52013
65/24/2013TX 16.00 52013
75/31/2013TX 20.00 5201344
86/1/2013TX 1.00 62013
96/4/2013TX 2.00 62013
106/5/2013TX 8.00 62013
116/6/2013TX 4.00 62013
126/11/2013TX 4.00 62013
136/11/2013TX 16.00 62013
146/14/2013TX 2.00 62013
156/14/2013TX 2.00 62013
166/14/2013TX 2.00 62013
176/24/2013TX 1.00 62013
186/24/2013TX 4.00 62013
196/24/2013TX 2.00 62013
206/25/2013TX 2.00 6201350
217/2/2013TX 4.00 72013
227/2/2013TX 1.00 72013
237/11/2013TX 4.00 72013
247/11/2013TX 6.00 72013
257/16/2013TX 5.00 72013
267/18/2013TX 1.00 7201321
278/20/2013TX 1.00 82013
288/23/2013TX 1.00 82013
298/23/2013TX 4.00 82013
308/26/2013TX 2.00 82013
318/27/2013TX 2.00 82013
328/30/2013TX 4.00 8201314

<colgroup><col style="width: 27pt; mso-width-source: userset; mso-width-alt: 1316;" width="36"> <col style="width: 48pt;" width="64"> <col style="width: 22pt; mso-width-source: userset; mso-width-alt: 1060;" width="29"> <col style="width: 57pt; mso-width-source: userset; mso-width-alt: 2779;" width="76"> <col style="width: 45pt; mso-width-source: userset; mso-width-alt: 2194;" width="60"> <col style="width: 48pt;" span="4" width="64"> <tbody>
</tbody>
 
Thanks for the compliments! I'll take a look at this later, but it seems trickier than the last part so I'm not so sure I'll be able to come up with anything. I'll give it a shot though hopefully I can figure it out.
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You're new to VBA? Are you kidding me? That code is great! It did exactly what I needed. :)
If you're new to VBA, you're going to be amazing pretty soon. But I think you're amazing already, I have been working on this code all day and got no where.
You must have some other kind of code background, because that code you provided is great.

Thanks very much!

You don't have to take on this next step I need, but while I have your attention, I thought I'd ask.

The next thing I need is to compile this data onto another worksheet where the Item numbers are placed in Column A, the descriptions go into Column B, then the totals for each month would be dropped into the respective columns to the right. The second sheet would have headers for each column. Like this;

ABCDEFGHIJKLMNO
1Item #DescriptionMay-13Jun-13Jul-13Aug-13Sep-13Oct-13Nov-13Dec-13Jan-14Feb-14Mar-14Apr-14May-14
210101Item 14450211445719303136428216
310102Item 2161011965231614104
410103Item 32712010206510
510104Item 40213020004420

<tbody>
</tbody>


Since I don't have all of your data I don't know for sure but I think this might help

Code:
Sub Fixed2()

Dim MyRange As Range
Dim CopyRange As Range
Dim Cumulative As Double
Dim StartRow As Integer
Dim PasteColumn As Integer, PasteRow As Integer




'// CHANGE THESE VALUES TO FIT YOUR NEEDS //
StartRow = 5   '//Row where data begins
ItemNumber = 2 '//Number of Items
'//////////////////////////////////////////


StartRow = 5
Cumulative = 0
PasteColumn = 3
PasteRow = 2
j = 2 'Counter variable


Set MyRange = Range("F:H").SpecialCells(xlCellTypeConstants)
lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row


For i = StartRow + 1 To lastrow + 1
    Cumulative = Cumulative + Range("D" & i - 1).Value
    If MyRange(i, 1) <> MyRange(i - 1, 1) Then
        Set CopyRange = MyRange(i - 1, 1).Offset(, 2)
        CopyRange.Value = Cumulative
        Cumulative = 0
    End If
Next i


'Copies Description
Range("C1:C5000").SpecialCells(xlCellTypeConstants).Copy ( _
        Worksheets(2).Range("B1"))


'Copies Item Numbers
Range("C1:C5000").SpecialCells(xlCellTypeConstants).Offset(0, -2).Copy ( _
        Worksheets(2).Range("A1"))


'Copies Numbers
For j = 1 To ItemNumber
    StartCopyRow = Cells.Find("Item " & (j), SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    
    If j = ItemNumber Then
        EndCopyRow = lastrow
    Else
        EndCopyRow = Cells.Find("Item " & (j + 1), SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    End If
        
    Range("H" & StartCopyRow, "H" & EndCopyRow).SpecialCells(xlCellTypeConstants).Copy
    Worksheets(2).Range("C" & j + 1).PasteSpecial Transpose:=True
Next j




End Sub

Just change the ItemNumber and StartRow values at the top. On the last macro it was StartRow + 1, I made a change so that's not needed anymore. Now, it's just wherever the data starts which in this case appears to be row 5. I also made the assumption that the second sheet in your workbook is where the data is summarized if that's not the case just change every instance of Worksheets(2) to Worksheets(x). I hope this works!
 
Upvote 0
RadioME,
This is really good stuff. Again, you are amazing.
Your entire code works, and pastes the data to the second worksheet with the sample data.

But I'm having a problem with this line;
Code:
For j = 1 To ItemNumber
    StartCopyRow = Cells.Find("Item " & (j), SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row

This line of code is searching for the item description (which are Item 1, Item 2, Item 3, etc) in the sample screen shots above. The code doesn't work with the real descriptions. Could you adjust this section so that the code searches for the item number, then transposes the relevant monthly data to sheet2?

If you are willing, I would not mind sending the actual file to you since files can't be attached in this forum.

Thanks,
Jim
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,890
Members
449,477
Latest member
panjongshing

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