Sum Multiple Columns Based on Multiple Criteria

aread

New Member
Joined
Dec 27, 2019
Messages
37
Office Version
  1. 365
Platform
  1. Windows
I am trying to consolidate data by summing columns G through M based on material, plant, vendor, and fiscal year being identical. The period does not matter. When summing columns G & I, the cell value is only the number even though it also shows the unit of measure when copied and pasted. I have removed our multiple vendors' names from column D due to confidentiality. I am hoping that someone will be able to assist because I keep hitting a brick wall. Thank you in advance.


MaterialPlantVendorVendor NameFiscal year/periodFiscal yearPO quantityOrder valueActual GR quantityGR valueInvoiced quantityInvoiced amountVar Inv Val vs PO valueUnit Price (Inv)
3000000122301005462JAN2017510,609 LB$ 146,527.87466,281 LB$ 133,598.54466,281 LB$ 134,712.33$ -11,815.54$ 0.29 / LB
3000000122301005462JUL201770,408 LB$ 21,524.7470,408 LB$ 21,524.7470,408 LB$ 21,798.97$ 274.23$ 0.31 / LB
3000000122301005462OCT2017192,092 LB$ 60,966.21141,904 LB$ 45,037.62141,904 LB$ 41,016.16$ -19,950.05$ 0.29 / LB
3000000122301005462DEC201771,966 LB$ 21,829.5771,966 LB$ 22,840.6971,966 LB$ 20,457.16$ -1,372.41$ 0.28 / LB
3000000122301005462FEB2018652,437 LB$ 208,916.42651,379 LB$ 205,397.84651,379 LB$ 216,981.38$ 8,064.96$ 0.33 / LB
3000000122301005462NOV2018132,000 LB$ 43,003.6269,339 LB$ 22,589.6769,339 LB$ 23,734.77$ -19,268.85$ 0.34 / LB
3000000122301005462JAN2019850,000 LB$ 266,149.75561,657 LB$ 180,367.64561,657 LB$ 169,588.80$ -96,560.95$ 0.30 / LB
3000000322301005462JAN20172,226,648 LB$ 581,034.262,226,648 LB$ 581,034.262,226,648 LB$ 591,520.58$ 10,486.32$ 0.27 / LB
3000000322301005462AUG20171,354,793 LB$ 387,965.431,302,618 LB$ 373,024.211,302,618 LB$ 355,510.42$ -32,455.01$ 0.27 / LB
3000000322301005462NOV2017504,907 LB$ 150,477.46504,907 LB$ 150,477.46504,907 LB$ 132,783.95$ -17,693.51$ 0.26 / LB
3000000322301005462FEB20181,994,822 LB$ 578,724.531,994,822 LB$ 558,457.981,994,822 LB$ 586,726.22$ 8,001.69$ 0.29 / LB
3000000322301005462OCT20181,138,598 LB$ 363,406.321,097,019 LB$ 350,135.541,097,019 LB$ 343,453.86$ -19,952.46$ 0.31 / LB
3000000322301005462JAN20191,973,753 LB$ 583,905.211,973,753 LB$ 583,905.211,973,753 LB$ 561,916.93$ -21,988.28$ 0.28 / LB
3000000322301005462APR2019388,151 LB$ 103,830.39388,151 LB$ 103,830.39388,151 LB$ 104,320.95$ 490.56$ 0.27 / LB
3000000322301005462NOV2019666,426 LB$ 144,947.66583,974 LB$ 127,014.34583,974 LB$ 127,014.24$ -17,933.42$ 0.22 / LB
3000000322301005462DEC20192,828,000 LB$ 615,090.00505,784 LB$ 110,008.03505,784 LB$ 104,473.99$ -510,616.01$ 0.21 / LB
3000000522301000562DEC2017148,490 LB$ 39,958.6684,000 LB$ 22,604.4084,000 LB$ 22,874.00$ -17,084.66$ 0.27 / LB
3000000522301005074NOV201842,000 LB$ 11,302.2042,000 LB$ 11,302.2042,000 LB$ 11,809.00$ 506.80$ 0.28 / LB
3000000522301005074APR2019106,490 LB$ 29,465.7884,000 LB$ 23,242.8084,000 LB$ 23,618.00$ -5,847.78$ 0.28 / LB
3000000522301005074DEC2019148,490 LB$ 42,572.080 LB$ 0.000 LB$ 0.00$ -42,572.08X
3000000622301004428AUG201730,240 LB$ 14,515.2015,000 LB$ 7,200.0015,000 LB$ 7,200.00$ -7,315.20$ 0.48 / LB
3000000622301004428NOV201751,260 LB$ 24,604.8045,000 LB$ 21,600.0045,000 LB$ 22,600.00$ -2,004.80$ 0.50 / LB
3000000622301004428NOV201860,000 LB$ 30,600.0060,000 LB$ 30,600.0060,000 LB$ 31,552.50$ 952.50$ 0.53 / LB
 
No, I want to keep the formatting and copy it to the newly consolidated data without having to go through the materials one by one to see which unit of measure applies.
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
No, I want to keep the formatting and copy it to the newly consolidated data without having to go through the materials one by one to see which unit of measure applies.

Sorry, but I still don't understand what you need.
Is it part of the macro? Or is it a new requirement?
 
Upvote 0
I wanted it as part of the same one if possible, but I haven't been able to figure it out. :(
 
Upvote 0
I wanted it as part of the same one if possible, but I haven't been able to figure it out.

But I don't understand what you need.
The data has "ton" in the number like "LB", if so, then change this line:

VBA Code:
cad2 = Replace(Replace(Replace(a(i, 7) & "|" & a(i, 8) & "|" & a(i, 9) & "|" & _
           a(i, 10) & "|" & a(i, 11) & "|" & a(i, 12) & "|" & a(i, 13), " LB", ""), " TON", ""), ",", "")
 
Upvote 0
In the original data, there is different number formatting depending on how the material is weighed such as #,##0" LB";-#,##0" LB";#,##0" LB" or #,##0" TON";-#,##0" TON";#,##0" TON" and I want the summed data to maintain the format of the original data without needing to go line by line manually if possible.
 
Upvote 0
Do you want the result of the sums to have the format "#,##0" LB";-#,##0" LB";#,##0" LB" or #,##0" TON";-#,##0" TON";#,##0" " on the "summary" sheet?

You can explain that with examples.

That is, the macro, can you add LB values with TON? or they are in different columns.
That's why I need you to explain it with examples.

If a column is always LB, would it be enough to change the format of the entire column to LB format?
Or how are your original data? because in your example of the initial requirement there are only values with LB; and apparently it is not a format, it is a text.
 
Upvote 0
It is definitely a format, not just text. When you click on the cell, you only see the number in the formula bar. In each of the columns "PO quantity" "Actual GR quantity" and "Invoiced quantity" there are some cells with "LB" as part of the number format and some with "TON"

Does that help?
 
Upvote 0
When summing columns G & I, the cell value is only the number even though it also shows the unit of measure when copied and pasted.

So is it text or format?
Why does your initial example appear as text?

there are some cells with "LB" as part of the number format and some with "TON"

Then the sum can contain values with "TON" and values with "LB" so what format should be put?
 
Upvote 0
Sorry for the confusion...

So is it text or format?
Why does your initial example appear as text?

It is format. "LB" and "TON" are part of the number format. It pastes the same as if it were formatted as Accounting with the $ even though the $ is not part of the actual number.

Then the sum can contain values with "TON" and values with "LB" so what format should be put?

It should retain the original formatting for the material number. Each material number will only have one type of number formatting for the quantity columns, but the column itself will contain multiple formats for different material numbers.
 
Last edited:
Upvote 0
Each material number will only have one type of number formatting

The process will take more time to format the rows.

Try this

VBA Code:
Sub Sum_Multiple_Columns()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, j As Long, a As Variant, b As Variant
  Dim cad1 As String, cad2 As String, cad3 As String
  Dim val1 As Variant, val2 As Variant, vSum As Double
  Dim dic As Object, ky As Variant, dic2 As Object
  Dim f As Range, frm As Variant, lr As Long
  Application.ScreenUpdating = False
  
  Set sh1 = Sheets("Sheet1")    'source data
  Set sh2 = Sheets("Summary")   'destination
  sh2.Rows("2:" & Rows.Count).ClearContents
  sh2.Range("A1").Value = sh1.Range("A1").Value
  
  Set dic = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  dic.CompareMode = vbTextCompare
  a = sh1.Range("A2:M" & sh1.Range("A" & Rows.Count).End(xlUp).Row).Value2
  
  For i = 1 To UBound(a, 1)
    dic2(a(i, 1)) = Empty
    cad1 = a(i, 1) & "|" & a(i, 2) & "|" & a(i, 3) & "|" & a(i, 6)
    cad2 = Replace(Replace(a(i, 7) & "|" & a(i, 8) & "|" & a(i, 9) & "|" & _
           a(i, 10) & "|" & a(i, 11) & "|" & a(i, 12) & "|" & a(i, 13), " LB", ""), ",", "")
        
    If Not dic.exists(cad1) Then
      dic(cad1) = cad2
    Else
      val1 = Split(dic(cad1), "|")
      val2 = Split(cad2, "|")
      cad3 = ""
      For j = 0 To UBound(val1)
        vSum = Val(val1(j)) + Val(val2(j))
        cad3 = cad3 & "|" & vSum
      Next
      dic(cad1) = Mid(cad3, 2)
    End If
  Next
  
  sh2.Range("A2").Resize(dic.Count).Value = Application.Transpose(dic.keys)
  sh2.Range("A2").Resize(dic.Count).TextToColumns sh2.Range("A2"), _
    xlDelimited, xlTextQualifierDoubleQuote, , , , , , True, "|"
  sh2.Range("E2").Resize(dic.Count).Value = Application.Transpose(dic.items)
  sh2.Range("E2").Resize(dic.Count).TextToColumns sh2.Range("E2"), _
    xlDelimited, xlTextQualifierDoubleQuote, , , , , , True, "|"

  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
  For Each ky In dic2
    frm = sh1.Range("A:A").Find(ky, , xlValues, xlWhole).Offset(, 6).NumberFormat
    sh2.Range("A1:K" & lr).AutoFilter 1, ky
    sh2.AutoFilter.Range.Range("E2:E" & lr & ",G2:G" & lr & ",I2:I" & lr).NumberFormat = frm
  Next
  sh2.ShowAllData
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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