How to combine some cells in different row with the same client and product codes

billmark

Board Regular
Joined
Dec 11, 2011
Messages
59
Client NoProduct CodeQuantityCostValue
456BOX84783008945.5810456.87
456215738400124847
515065656850810000416548794163
515287132P10240041820.0444000.58
45621573880011.251548.69

<tbody>
</tbody>











Hi
The above is the extract from a data worksheet which has over 15K+ rows and 50 columns. I want to use VBA macro to combine the quantity, cost and value if the client and product is the same.

eg client 456 has the same product 215738, combine quantity, cost and value to one of the row then delete the row.

Client NoProduct CodeQuantityCostValue
456BOX84783008945.5810456.87
45621573812004111948
515065656850810000416548794163
515287132P10240041820.0444000.58

<tbody>
</tbody>









Please help how to as I have used the excel function such as sort the client then product, manually add and delete the row.

Regards
Bill
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
billmark,

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

2. Are you using a PC or a Mac?

Here is a macro solution for you to consider.

You can change the raw data worksheet name in the macro.

Sample raw data:


Excel 2007
ABCDE
1Client NoProduct CodeQuantityCostValue
2456BOX84783008945.5810456.87
3456215738400124847
4515065656850810000416548794163
5515287132P10240041820.0444000.58
645621573880011.251548.69
7
Sheet1


After the macro:


Excel 2007
ABCDE
1Client NoProduct CodeQuantityCostValue
2456215738120023.256395.69
3456BOX84783008945.5810456.87
4515065656850810000416548794163
5515287132P10240041820.0444000.58
6
7
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, 07/03/2015, ME865803
Dim lr As Long, r As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  .Range("A2:E" & lr).Sort key1:=.Range("A2"), order1:=1, key2:=.Range("B2"), order2:=1
  With .Range("F2:F" & lr)
    .Formula = "=A2&B2"
    .Value = .Value
  End With
  For r = 2 To lr
    n = Application.CountIf(.Columns(6), .Cells(r, 6).Value)
    If n > 1 Then
      .Range("C" & r).Value = Evaluate("=Sum(C" & r & ":C" & r + n - 1 & ")")
      .Range("D" & r).Value = Evaluate("=Sum(D" & r & ":D" & r + n - 1 & ")")
      .Range("E" & r).Value = Evaluate("=Sum(E" & r & ":E" & r + n - 1 & ")")
      .Range("A" & r + 1 & ":E" & r + n - 1).ClearContents
    End If
    r = r + n - 1
  Next r
  .Range("F2:F" & lr).ClearContents
  .Range("A2:E" & lr).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
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, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ConsolidateData macro.
 
Upvote 0
billmark,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Client No
Product CodeQuantityCostValueother
456BOX84783008945.5810456.87457
4562157384005454847
515065656850810000416548794163799
515287132P10240041820.0444000.58
45621573880054.51548.69457

<colgroup><col style="mso-width-source:userset;mso-width-alt:2112;width:50pt" width="66"> <col style="mso-width-source:userset;mso-width-alt:3072;width:72pt" width="96"> <col style="mso-width-source:userset;mso-width-alt:2048;width:48pt" width="64"> <col style="mso-width-source:userset;mso-width-alt:2272; width:53pt" span="2" width="71"> <col style="width:54pt" width="72"> </colgroup><tbody>
</tbody>

Hi
I've added one column 'other' and some of the cells are blank. After running the macro (modified the macro to include the new column or row), the cell under the column 'other' shifted up. Please advise how to modify the code so as to delete the whole blank row instead of the blank cell.

Regards
 
Upvote 0
billmark,

Here is a new macro solution for you to consider based on the new raw data structure.

You can change the raw data worksheet name in the macro.

New sample raw data:


Excel 2007
ABCDEF
1Client NoProduct CodeQuantityCostValueother
2456BOX84783008945.5810456.87457
34562157384005454847
4515065656850810000416548794163799
5515287132P10240041820.0444000.58
645621573880054.51548.69457
7
Sheet1


After the new macro:


Excel 2007
ABCDEF
1Client NoProduct CodeQuantityCostValueother
24562157381200599.56395.69457
3456BOX84783008945.5810456.87457
4515065656850810000416548794163799
5515287132P10240041820.0444000.58
6
7
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).

Code:
Sub ConsolidateData_V2()
' hiker95, 08/26/2015, ME865803
Dim lr As Long, r As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  .Range("A2:F" & lr).Sort key1:=.Range("A2"), order1:=1, key2:=.Range("B2"), order2:=1
  With .Range("G2:G" & lr)
    .Formula = "=A2&B2"
    .Value = .Value
  End With
  For r = 2 To lr
    n = Application.CountIf(.Columns(7), .Cells(r, 7).Value)
    If n > 1 Then
      .Range("C" & r).Value = Evaluate("=Sum(C" & r & ":C" & r + n - 1 & ")")
      .Range("D" & r).Value = Evaluate("=Sum(D" & r & ":D" & r + n - 1 & ")")
      .Range("E" & r).Value = Evaluate("=Sum(E" & r & ":E" & r + n - 1 & ")")
      .Range("F" & r).Value = Evaluate("=Sum(F" & r & ":F" & r + n - 1 & ")")
      .Range("A" & r + 1 & ":F" & r + n - 1).ClearContents
    End If
    r = r + n - 1
  Next r
  .Range("G2:G" & lr).ClearContents
  .Range("A2:F" & lr).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
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, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ConsolidateData_V2 macro.


If the above is not correct, because you have not supplied a screenshot of what the new results should look like, then:

Please supply a screenshot of what the correct results should look like.
 
Upvote 0
Hi
I have the example for your reference
The original table to be used:
ClientProduct CodeQuantityCostMarket Valueotherother1other2other3other4other5
CLTA4569.69896.9896.985510546654654654
CLTA25462658015.453413316.33476938.861
CLTAEJRWER100130.01130.811
CLTA0OWE9R-2074.9-2702.37-2714.171
CLTC000324AA121156702362088.782746742.8199.25000324AA1000324AA1BNGK54312
CLTC001272AA131514743700336.154053772.522128.631001272AA1001272AA1B1VSYJ66.032
CLTC00130HBS3560000563803.7803955.68109.7500130HBS300130HBS3B8J7VH17.375
CLTC
00206R1022466009332522.0911206305.02 34.7400206R10200206R1022831811
CLTC00206R102965003988226.124385273.46 34.7400206R10200206R1022831811
CLTC00208D4082423005439409.91474181119.5700208D40800208D408B6463M8
CLTC00289RAA028220002990590.293525331.2895.500289RAA000289RAA0B5BDZ748.875
CLTC00289VAB934220003826662.43525089.2878.7500289VAB900289VAB900289VAB97.75

<colgroup><col><col><col><col><col><col><col><col span="2"><col><col></colgroup><tbody>
</tbody>

After running the macro:
Sub ConsolidateData()
' hiker95, 07/03/2015, ME865803
Dim lr As Long, r As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1") '<-- you can change the sheet name here
lr = .Cells(Rows.Count, 1).End(xlUp).Row
.Range("A2:k" & lr).Sort key1:=.Range("A2"), order1:=1, key2:=.Range("b2"), order2:=1
With .Range("l2:l" & lr)
.Formula = "=A2&b2"
.Value = .Value
End With
For r = 2 To lr
n = Application.CountIf(.Columns(12), .Cells(r, 12).Value)
If n > 1 Then
.Range("c" & r).Value = Evaluate("=Sum(c" & r & ":c" & r + n - 1 & ")")
.Range("d" & r).Value = Evaluate("=Sum(d" & r & ":d" & r + n - 1 & ")")
.Range("e" & r).Value = Evaluate("=Sum(e" & r & ":e" & r + n - 1 & ")")
.Range("A" & r + 1 & ":k" & r + n - 1).ClearContents
End If
r = r + n - 1
Next r
.Range("l2:l" & lr).ClearContents
.Range("A2:k" & lr).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
End With
Application.ScreenUpdating = True
End Sub

But the result, other2, other3, other4 and other5 have shifted up:
ClientProduct CodeQuantityCostMarket Valueotherother1other2other3other4other5
CLTA0OWE9R-2074.9-2702.37-2714.1755154665465465412
CLTA25462658015.453413316.33476938.8621000324AA1000324AA1BNGK5436.032
CLTA4569.69896.9896.9810001272AA1001272AA1B1VSYJ67.375
CLTAEJRWER100130.01130.81100130HBS300130HBS3B8J7VH18.875
CLTC000324AA121156702362088.782746742.8199.2500206R102
00206R10228318117.75
CLTC001272AA131514743700336.154053772.52128.63100208D40800208D408B6463M8
CLTC00130HBS3560000563803.7803955.68109.7500289RAA000289RAA0B5BDZ74
CLTC
00206R10234310013320748.215591578.48
34.7400289VAB900289VAB900289VAB9
CLTC00208D4082423005439409.91474181119.57
CLTC00289RAA028220002990590.293525331.2895.5
CLTC00289VAB934220003826662.43525089.2878.75

<colgroup><col><col><col><col><col><col><col><col span="2"><col><col></colgroup><tbody>
</tbody>

I expect the outcome:
ClientProduct CodeQuantityCostMarket Valueotherother1other2other3other4other5
CLTA0OWE9R-2074.9-2702.37-2714.171
CLTA25462658015.453413316.33476938.861
CLTA4569.69896.9896.985510546654654654
CLTAEJRWER100130.01130.811
CLTC000324AA121156702362088.782746742.8199.25000324AA1000324AA1BNGK54312
CLTC001272AA131514743700336.154053772.522128.631001272AA1001272AA1B1VSYJ66.032
CLTC00130HBS3560000563803.7803955.68109.7500130HBS300130HBS3B8J7VH17.375
CLTC
00206R10234310013320748.215591578.48 34.7400206R10200206R1022831811
CLTC00208D4082423005439409.91474181119.5700208D40800208D408B6463M8
CLTC00289RAA028220002990590.293525331.2895.500289RAA000289RAA0B5BDZ748.875
CLTC00289VAB934220003826662.43525089.2878.7500289VAB900289VAB900289VAB97.75

<colgroup><col><col><col><col><col><col><col><col span="2"><col><col></colgroup><tbody>
</tbody>
Please advise how to fix the code.

Regards
Bill
 
Upvote 0
billmark,

Here is NEW macro solution for you to consider based on the NEW raw data structure you have displayed.

You can change the raw data worksheet name in the macro.

New sample raw data:


Excel 2007
ABCDEFGHIJK
1ClientProduct CodeQuantityCostMarket Valueotherother1other2other3other4other5
2CLTA4569.69896.9896.985510546654654654
3CLTA25462658015.453413316.33476938.861
4CLTAEJRWER100130.01130.811
5CLTA0OWE9R-2074.9-2702.37-2714.171
6CLTC000324AA121156702362088.782746742.8199.25000324AA1000324AA1BNGK54312
7CLTC001272AA131514743700336.154053772.522128.631001272AA1001272AA1B1VSYJ66.032
8CLTC00130HBS3560000563803.7803955.68109.7500130HBS300130HBS3B8J7VH17.375
9CLTC00206R1022466009332522.0911206305.0234.7400206R10200206R1022831811
10CLTC00206R102965003988226.124385273.4634.7400206R10200206R1022831811
11CLTC00208D4082423005439409.91474181119.5700208D40800208D408B6463M8
12CLTC00289RAA028220002990590.293525331.2895.500289RAA000289RAA0B5BDZ748.875
13CLTC00289VAB934220003826662.43525089.2878.7500289VAB900289VAB900289VAB97.75
14
Sheet1


After the new macro:


Excel 2007
ABCDEFGHIJK
1ClientProduct CodeQuantityCostMarket Valueotherother1other2other3other4other5
2CLTA4569.69896.9896.985510546654654654
3CLTA25462658015.453413316.33476938.861
4CLTA0OWE9R-2074.9-2702.37-2714.171
5CLTAEJRWER100130.01130.811
6CLTC000324AA121156702362088.782746742.8199.25000324AA1000324AA1BNGK54312
7CLTC001272AA131514743700336.154053772.522128.631001272AA1001272AA1B1VSYJ66.032
8CLTC00130HBS3560000563803.7803955.68109.7500130HBS300130HBS3B8J7VH17.375
9CLTC00206R10234310013320748.215591578.4834.7400206R10200206R1022831811
10CLTC00208D4082423005439409.91474181119.5700208D40800208D408B6463M8
11CLTC00289RAA028220002990590.293525331.2895.500289RAA000289RAA0B5BDZ748.875
12CLTC00289VAB934220003826662.43525089.2878.7500289VAB900289VAB900289VAB97.75
13
14
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).

Code:
Sub ConsolidateData_V3()
Dim lr As Long, r As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  .Range("A2:K" & lr).Sort key1:=.Range("A2"), order1:=1, key2:=.Range("B2"), order2:=1
  With .Range("L2:L" & lr)
    .Formula = "=A2&B2"
    .Value = .Value
  End With
  For r = 2 To lr
    n = Application.CountIf(.Columns(12), .Cells(r, 12).Value)
    If n > 1 Then
      .Range("C" & r).Value = Evaluate("=Sum(C" & r & ":C" & r + n - 1 & ")")
      .Range("D" & r).Value = Evaluate("=Sum(D" & r & ":D" & r + n - 1 & ")")
      .Range("E" & r).Value = Evaluate("=Sum(E" & r & ":E" & r + n - 1 & ")")
      .Range("A" & r + 1 & ":A" & r + n - 1).ClearContents
    End If
    r = r + n - 1
  Next r
  .Range("L2:L" & lr).ClearContents
  lr = .Cells(Rows.Count, 2).End(xlUp).Row
  For r = lr To 2 Step -1
    If .Range("A" & r) = vbEmpty Then .Rows(r).Delete
  Next r
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, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ConsolidateData_V3 macro.
 
Upvote 0
billmark,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,854
Members
449,051
Latest member
excelquestion515

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