VBA code snippet to consolidate/Sum

F3hunter

New Member
Joined
Jan 17, 2015
Messages
18
Hi,

I am your average Excel user having a hard time trying an end result that will work for me. Horrid VBA skills here. I have tried Pivot Tables (Too busy), I ave tried SUMIFS as well to no avail.

If someone could help that would be awesome. I am looking for code to do the following,
Consolidate the redundant items in Product column A, sum the values from those in columns Pounds D and Total F. All this and keep the consolidation/SUM to each specific category.

Each product item can be in a different "Category" which is what is giving me a headache trying to figure this out.

What it looks like before consolidation:

Product CodeDescriptionCategoryPoundsPriceTotal
1111SC12NBCBER40.00$3.40$136.00
1111SC12NBCBER20.00$3.40$68.00
1111SC12NBCBER40.00$3.40$136.00
1111SC12NBCBER100.00$3.40$340.00
1111SC12NBCBER20.00$3.40$68.00
1112NLR12NBCBER20.00$6.22$124.40
1112NLR12NBCBER20.00$6.22$124.40
1113Wh12NBCBER300.00$2.95$885.00
1113Wh12NBCBER200.00$2.95$590.00
1113Wh12NBCBER200.00$2.95$590.00
1113Wh12NBCBER700.00$2.95$2065.00
1114Bl112NBCBER60.00$10.18$610.80
1114Bl112NBCBER100.00$10.18$1018.00
1114Bl112NBCBER340.00$10.18$3461.20
1115Bl212NBCBER80.00$10.18$814.40
1115Bl212NBCBER80.00$10.18$814.40
1115Bl212NBCBER300.00$10.18$3054.00
1115Bl212NBCBER40.00$10.18$407.20
1116293512NBCBER20.00$13.50$270.00
1117G12NBCBER91.00$5.68$516.88
1117G12NBCBER40.00$5.68$227.20
1117G12NBCBER40.00$5.68$227.20
1117G12NBCBER100.00$5.68$568.00
1118Mod Bl12NBCBER60.00$9.67$580.20
1118Mod Bl12NBCBER40.00$9.67$386.80
1118Mod Bl12NBCBER40.00$9.67$386.80
1118Mod Bl12NBCBER120.00$9.67$1160.40
1119CY12NBCSD20.00$6.52$130.40
1119CY12NBCSD60.00$6.52$391.20
1111SC12NBCSD40.00$3.40$136.00
1113Wh12NBCSD50.00$2.95$147.50
1113Wh12NBCSD25.00$2.95$73.75
1113Wh12NBCSD50.00$2.95$147.50
1113Wh12NBCSD150.00$2.95$442.50
1120RBB12NBCSD20.00$6.17$123.40
1121BW12NBCSD25.00$3.97$99.25
1122Br12NBCSD160.00$13.50$2160.00
1123RB12NBCSD40.00$6.17$246.80
1124DG12NBCSD17.00$13.45$228.65
1125DB12NBCSD20.00$9.15$183.00
1125DB12NBCSD40.00$9.15$366.00
1125DB12NBCSD20.00$9.15$183.00
1125DB12NBCSD20.00$9.15$183.00
1126Grove12NBCSD17.00$6.70$113.90
1127Hc12NBCSD10.00$6.17$61.70
1128Ha12NBCSD40.00$7.61$304.40
1129Fr12NBCSD10.00$13.45$134.50
1130Pr12NBCSD20.00$6.38$127.60
1130Pr12NBCSD20.00$6.38$127.60
1130Pr12NBCSD40.00$6.38$255.20
1131Pb12NBCSD20.00$9.67$193.40
1131Pb12NBCSD40.00$9.67$386.80
1131Pb12NBCSD60.00$9.67$580.20
1132MRR12NBCSD20.00$6.70$134.00
113322012NBCSD80.00$6.70$536.00
1134PBB12NBCSD260.00$10.89$2831.40
1134PBB12NBCSD140.00$10.89$1524.60
1134PBB12NBCSD300.00$10.89$3267.00
1111SC16NBCBER20.00$3.40$68.00
1114Bl116NBCBER60.00$10.18$610.80
1115Bl216NBCBER80.00$10.18$814.40
1135MLG16NBCSD10.00$7.61$76.10
1121BW16NBCSD25.00$3.97$99.25
1125DB16NBCSD60.00$9.15$549.00
1135pp12BCBER20.00$7.65$153.00

<tbody>
</tbody>

What I need it to like like after:

Product CodeDescriptionCategoryPoundsPriceTotal
1111SC12NBCBER220.00$ 3.40$748.00
1112NLR12NBCBER40.00$ 6.22$248.80
1113Wh12NBCBER1400.00$ 2.95$4130.00
1135pp12BCBER20.00$ 7.65$153.00
1119CY12NBCSD80.00$ 6.52$521.60
1113Wh12NBCSD275.00$ 2.95$811.25

<tbody>
</tbody>

Thanks in advance for any help you can offer.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
After scouring the internet I located several snippets and have tried to break them down to understand them. I have put this code together and it partially works, but does not seem to be grabbing all the data in column A and is not summing the duplicate data.



Here is the code that I have sewn together:

Private Sub CommandButton21_Click()


Set wsCopy = Sheets("Sheet1")
Set wsPaste = Sheets("Sheet2")
lBR = wsCopy.Cells(Application.Rows.Count, "A").End(xlUp).Row
Set rCopy = wsCopy.Range("A1:F" & lBR)
Set rPaste = wsPaste.Range("A1:F" & lBR)
rCopy.Copy rPaste
rPaste.RemoveDuplicates Columns:=Array(1), Header _
:=xlYes
wsPaste.Range("D:D").Formula = "=SUMIFS(Sheet1!D:D,Sheet1!A:A,Sheet1!C:C,Sheet2!C:C)"
wsPaste.Range("F:F").Formula = "=SUMIFS(Sheet1!F:F,Sheet1!A:A,Sheet1!C:C,Sheet2!C:C)"
lBR = wsPaste.Cells(Application.Rows.Count, "A").End(xlUp).Row


Set rPaste = wsPaste.Range("A1:F" & lBR)
rPaste.Copy
rPaste.PasteSpecial xlPasteValues




End Sub
 
Last edited:
Upvote 0
F3hunter,

Welcome to the MrExcel forum.

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


Sample raw data in worksheet Sheet1 (the worksheet name can be changed in the macro):


Excel 2007
ABCDEF
1Product CodeDescriptionCategoryPoundsPriceTotal
21111SC12NBCBER40$3.40$136.00
31111SC12NBCBER20$3.40$68.00
41111SC12NBCBER40$3.40$136.00
51111SC12NBCBER100$3.40$340.00
61111SC12NBCBER20$3.40$68.00
71112NLR12NBCBER20$6.22$124.40
81112NLR12NBCBER20$6.22$124.40
91113Wh12NBCBER300$2.95$885.00
101113Wh12NBCBER200$2.95$590.00
111113Wh12NBCBER200$2.95$590.00
121113Wh12NBCBER700$2.95$2,065.00
131114Bl112NBCBER60$10.18$610.80
141114Bl112NBCBER100$10.18$1,018.00
151114Bl112NBCBER340$10.18$3,461.20
161115Bl212NBCBER80$10.18$814.40
171115Bl212NBCBER80$10.18$814.40
181115Bl212NBCBER300$10.18$3,054.00
191115Bl212NBCBER40$10.18$407.20
201116293512NBCBER20$13.50$270.00
211117G12NBCBER91$5.68$516.88
221117G12NBCBER40$5.68$227.20
231117G12NBCBER40$5.68$227.20
241117G12NBCBER100$5.68$568.00
251118Mod Bl12NBCBER60$9.67$580.20
261118Mod Bl12NBCBER40$9.67$386.80
271118Mod Bl12NBCBER40$9.67$386.80
281118Mod Bl12NBCBER120$9.67$1,160.40
291119CY12NBCSD20$6.52$130.40
301119CY12NBCSD60$6.52$391.20
311111SC12NBCSD40$3.40$136.00
321113Wh12NBCSD50$2.95$147.50
331113Wh12NBCSD25$2.95$73.75
341113Wh12NBCSD50$2.95$147.50
351113Wh12NBCSD150$2.95$442.50
361120RBB12NBCSD20$6.17$123.40
371121BW12NBCSD25$3.97$99.25
381122Br12NBCSD160$13.50$2,160.00
391123RB12NBCSD40$6.17$246.80
401124DG12NBCSD17$13.45$228.65
411125DB12NBCSD20$9.15$183.00
421125DB12NBCSD40$9.15$366.00
431125DB12NBCSD20$9.15$183.00
441125DB12NBCSD20$9.15$183.00
451126Grove12NBCSD17$6.70$113.90
461127Hc12NBCSD10$6.17$61.70
471128Ha12NBCSD40$7.61$304.40
481129Fr12NBCSD10$13.45$134.50
491130Pr12NBCSD20$6.38$127.60
501130Pr12NBCSD20$6.38$127.60
511130Pr12NBCSD40$6.38$255.20
521131Pb12NBCSD20$9.67$193.40
531131Pb12NBCSD40$9.67$386.80
541131Pb12NBCSD60$9.67$580.20
551132MRR12NBCSD20$6.70$134.00
56113322012NBCSD80$6.70$536.00
571134PBB12NBCSD260$10.89$2,831.40
581134PBB12NBCSD140$10.89$1,524.60
591134PBB12NBCSD300$10.89$3,267.00
601111SC16NBCBER20$3.40$68.00
611114Bl116NBCBER60$10.18$610.80
621115Bl216NBCBER80$10.18$814.40
631135MLG16NBCSD10$7.61$76.10
641121BW16NBCSD25$3.97$99.25
651125DB16NBCSD60$9.15$549.00
661135pp12BCBER20$7.65$153.00
67
Sheet1


After the macro:


Excel 2007
IJKLMN
1Product CodeDescriptionCategoryPoundsPriceTotal
21111SC12NBCBER220$3.40$748.00
31111SC12NBCSD40$3.40$136.00
41111SC16NBCBER20$3.40$68.00
51112NLR12NBCBER40$6.22$248.80
61113Wh12NBCBER1400$2.95$4,130.00
71113Wh12NBCSD275$2.95$811.25
81114Bl112NBCBER500$10.18$5,090.00
91114Bl116NBCBER60$10.18$610.80
101115Bl212NBCBER500$10.18$5,090.00
111115Bl216NBCBER80$10.18$814.40
121116293512NBCBER20$13.50$270.00
131117G12NBCBER271$5.68$1,539.28
141118Mod Bl12NBCBER260$9.67$2,514.20
151119CY12NBCSD80$6.52$521.60
161120RBB12NBCSD20$6.17$123.40
171121BW12NBCSD25$3.97$99.25
181121BW16NBCSD25$3.97$99.25
191122Br12NBCSD160$13.50$2,160.00
201123RB12NBCSD40$6.17$246.80
211124DG12NBCSD17$13.45$228.65
221125DB12NBCSD100$9.15$915.00
231125DB16NBCSD60$9.15$549.00
241126Grove12NBCSD17$6.70$113.90
251127Hc12NBCSD10$6.17$61.70
261128Ha12NBCSD40$7.61$304.40
271129Fr12NBCSD10$13.45$134.50
281130Pr12NBCSD80$6.38$510.40
291131Pb12NBCSD120$9.67$1,160.40
301132MRR12NBCSD20$6.70$134.00
31113322012NBCSD80$6.70$536.00
321134PBB12NBCSD700$10.89$7,623.00
331135MLG16NBCSD10$7.61$76.10
341135pp12BCBER20$7.65$153.00
35
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ConsolidateData()
' hiker95, 01/17/2015, ME829707
Dim oa As Variant
Dim lr As Long, r As Long, nr As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")
  .Columns("I:N").ClearContents
  .Cells(1, 9).Resize(, 6).Value = .Cells(1, 1).Resize(, 6).Value
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  oa = .Range("A2:F" & lr)
  With .Range("G2:G" & lr)
    .Formula = "=A2&B2&C2"
    .Value = .Value
  End With
  .Range("A2:G" & lr).Sort key1:=.Range("G2"), order1:=1
  nr = 1
  For r = 2 To lr
    n = Application.CountIf(.Columns(7), .Cells(r, 7).Value)
    nr = nr + 1
    If n = 1 Then
      .Cells(nr, 9).Resize(, 6).Value = .Cells(r, 1).Resize(, 6).Value
    Else
      .Cells(nr, 9).Resize(, 3).Value = .Cells(r, 1).Resize(, 3).Value
      .Cells(nr, 12).Value = Evaluate("=Sum(D" & r & ":D" & r + n - 1 & ")")
      .Cells(nr, 13).Value = .Cells(r, 5).Value
      .Cells(nr, 14).Value = Evaluate("=Sum(F" & r & ":F" & r + n - 1 & ")")
    End If
    r = r + n - 1
  Next r
  .Range("M2:M" & nr).NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
  .Range("N2:N" & nr).NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
  .Range("A2:F" & lr) = oa
  .Range("G2:G" & lr).ClearContents
  .Columns("I:N").AutoFit
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ConsolidateData macro.
 
Upvote 0
Hiker95-Wow..Worked perfectly, I've spent days on this and must extend my sincerest gratitude. I'll study that code to get a better understanding of how it works so when the next project comes I will be better prepared. Have a great weekend!

Windows 8.1
Excel 2010

F3
 
Upvote 0
F3hunter,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.


I missed your posted macro - you are using two worksheets.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ConsolidateData_V2()
' hiker95, 01/17/2015, ME829707
Dim w1 As Worksheet, w2 As Worksheet
Dim oa As Variant
Dim lr As Long, r As Long, nr As Long, n As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
With w2
  .UsedRange.ClearContents
  .Range("A1").Resize(, 6).Value = w1.Range("A1").Resize(, 6).Value
End With
With w1
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  oa = .Range("A2:F" & lr)
  With .Range("G2:G" & lr)
    .Formula = "=A2&B2&C2"
    .Value = .Value
  End With
  .Range("A2:G" & lr).Sort key1:=.Range("G2"), order1:=1
  nr = 1
  For r = 2 To lr
    n = Application.CountIf(.Columns(7), .Cells(r, 7).Value)
    nr = nr + 1
    If n = 1 Then
      w2.Cells(nr, 1).Resize(, 6).Value = w1.Cells(r, 1).Resize(, 6).Value
    Else
      w2.Cells(nr, 1).Resize(, 3).Value = w1.Cells(r, 1).Resize(, 3).Value
      w2.Cells(nr, 4).Value = Evaluate("=Sum(D" & r & ":D" & r + n - 1 & ")")
      w2.Cells(nr, 5).Value = w1.Cells(r, 5).Value
      w2.Cells(nr, 6).Value = Evaluate("=Sum(F" & r & ":F" & r + n - 1 & ")")
    End If
    r = r + n - 1
  Next r
  w2.Range("E2:E" & nr).NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
  w2.Range("F2:F" & nr).NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
  .Range("A2:F" & lr) = oa
  .Range("G2:G" & lr).ClearContents
  w2.Columns("A:F").AutoFit
End With
w2.Activate
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ConsolidateData_V2 macro.
 
Upvote 0
Yes, I was using 2 worksheets and thank you for noticing that and spending the time to change it up. Again, it worked perfectly!

My wife is happy as well! Spend a little time with her this weekend now.:eek:
 
Upvote 0
F3hunter,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Similar threads

Forum statistics

Threads
1,215,777
Messages
6,126,834
Members
449,343
Latest member
DEWS2031

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