VBA code to shrink the rows by cumilating the data

Wafee

Board Regular
Joined
May 27, 2020
Messages
104
Office Version
  1. 2013
Platform
  1. Windows
I have a excel sheet with around 5lakh rows and i want to reduce number of rows by cumilating same data.
below is the sample data (Couldn't share original data due to privacy reasons). Data should be combined based on Columns B,D,E,G.

If data present in B,D,E,F columns is same the code has to sum the values in G column and make it in to single row of data as shown in the next table.
Original data is not sorted and output expected is given below.
Would be great if Output can be obtained be in a new sheet in the same workbook leaving orinal data as it is.

ABCDEFG
City CodeCity NamePincodeDateTypeDescriptionTotal
1NewYork321-Jan-20SAon40
1NewYork321-Jan-20SAon40
1NewYork321-Jan-20NAoff20
2Sydney4420-Jan-20NAoff40
2Sydney4420-Jan-20NAoff40
3Delhi9420-Jan-20SAon20
3Delhi9420-Jan-20SAon20
3Delhi9420-Jan-20SAon40

Out put expected is below.

City CodeCity NamePincodeDateTypeDescriptionTotal
1NewYork321-Jan-20SAon80
1NewYork321-Jan-20NAoff20
2Sydney4420-Jan-20NAoff80
3Delhi9420-Jan-20SAon80
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Using Power Query which you can download from MS as an add in for your version of Excel. ( It is native to 2016 and later).

Here is the Mcode

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"City Code", Int64.Type}, {"City Name", type text}, {"Pincode", Int64.Type}, {"Date", type date}, {"Type", type text}, {"Description", type text}, {"Total", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"City Name", "Date", "Type", "Description"}, {{"Total", each List.Sum([Total]), type nullable number}})
in
    #"Grouped Rows"

Book2
ABCDE
1City NameDateTypeDescriptionTotal
2NewYork1/1/2020SAon80
3NewYork1/1/2020NAoff20
4Sydney1/20/2020NAoff80
5Delhi1/20/2020SAon80
Sheet2
 
Upvote 0
Using Power Query which you can download from MS as an add in for your version of Excel. ( It is native to 2016 and later).

Here is the Mcode

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"City Code", Int64.Type}, {"City Name", type text}, {"Pincode", Int64.Type}, {"Date", type date}, {"Type", type text}, {"Description", type text}, {"Total", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"City Name", "Date", "Type", "Description"}, {{"Total", each List.Sum([Total]), type nullable number}})
in
    #"Grouped Rows"

Book2
ABCDE
1City NameDateTypeDescriptionTotal
2NewYork1/1/2020SAon80
3NewYork1/1/2020NAoff20
4Sydney1/20/2020NAoff80
5Delhi1/20/2020SAon80
Sheet2

Hi Alan,

I have installed power query now. can you help on how to run above code? I tried to run it in VB editor but it was not possible.
My workbook name is "wb1" and data is in the sheet named "Source" also have an empty sheet named "output" for the output.
 
Upvote 0

 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,617
Members
449,039
Latest member
Mbone Mathonsi

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