Merge Data from multiple sheets into one by transposing and counting(VBA)

coolguyvarun01

New Member
Joined
Jan 5, 2015
Messages
27
Office Version
  1. 2019
Platform
  1. Windows
Hi Experts,

Got a requirement from my manager that have to merge data of multiple sheets into one by transposing the dates and generating columns based on different values and then showing total count in the respective column and all through the click of a button.

Kindly help.

Attached is the workbook with 4 data sheets and 1 summary sheet, Summary sheet is supposed to be generated by clicking the button in the sheet.

Kindly help.

Thank You in Advance

OutPut

problem statement.xlsm
ABCDEF
1
2
3DatePACLPLWFH
401-05-2021400000
502-05-2021364000
603-05-2021324400
704-05-2021364000
805-05-20212412040
906-05-2021400000
1007-05-2021288400
1108-05-2021364000
1209-05-2021324004
1310-05-2021324004
Required Output


Sample Data


problem statement.xlsm
ABCDEFGHIJKLMN
1NameAgeTargetJoining Date01-05-202102-05-202103-05-202104-05-202105-05-202106-05-202107-05-202108-05-202109-05-202110-05-2021
2aaaa241013PPPPPPPPPP
3bbbb432809PPPPAPAAPA
4cccc373946PPPPPLPCLPWFHWFH
5dddd362917PPAPPPPPPP
6eeee212053PPPPAPPPPP
7ffff383723PPPPPPPPPP
8gggg371630PACLAAPAPAP
9hhhh251781PPPPPPPPPP
10iiii323146PPPPPPPPPP
11jjjj402596PPPPPPPPPP
San Christiano
 

Attachments

  • Los Anngeles.JPG
    Los Anngeles.JPG
    97.8 KB · Views: 8
  • output.JPG
    output.JPG
    58.2 KB · Views: 8
  • san christiano.JPG
    san christiano.JPG
    102 KB · Views: 7
  • San Hose.JPG
    San Hose.JPG
    101.8 KB · Views: 6
  • San Ramon.JPG
    San Ramon.JPG
    99.4 KB · Views: 7

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I would recommend using Power Query and it would be good to know how you get the data to this point.
If your source system is creating 4 csv files which you are manually copying into this workbook, it would be better to get Power Query import the source files directly.
Can you please clarify how the data is getting into this workbook ?
If they are csv files how are they named, and what tells you which file is for which city.
 
Upvote 0
If you are interested in a Power Query (PQ) solution and you are happy to have the summary report in a different workbook below is one option.
Note: If you want it to be in the same workbook we would need each sheet to have the data area converted to a table, which one of the reasons I wanted to know how you get to that point and as a follow on how you refresh the data. A better way would be to read in all the initial source files if that is how you get the data in the first place.

Here are the steps to implement it as is with the summary in a different workbook
1) Create a new workbook
2) Create an Excel Table in that workbook and call it Parameters
3) Enter the full path and file name for the Workbook with your source data in it.
20210509 PQ Merge Sheets and transpose Combined v02.xlsx
AB
1ParameterValue
2FullFileNameC:\PQ\20210509 PQ Merge Sheets and transpose v02.xlsx
Required Output


4) now go into Power Query via Data > Get Data > From Other Sources > Blank Query
5) In the white Name box on the right hand side change it from Query1 to fnGetParameter
6) On the home tab select Advanced Editor (3rd from the right)
7) Select All in the editor and replace what you see with this.
Power Query:
(ParameterName as text) =>
let
    ParamSource = Excel.CurrentWorkbook(){[Name="Parameters"]}[Content],
    ParamRow = Table.SelectRows(ParamSource, each ([Parameter] = ParameterName )),
    Value=
        if Table.IsEmpty(ParamRow)=true
        then null
        else Record.Field(ParamRow{0},"Value")
in
    Value

8) In the Query Pane on the left, right click in the grey area and select Neq Query > Other Sources > Blank Query
9) On the home tab select Advanced Editor (3rd from the right)
10) Select All in the editor and replace what you see with this.
Power Query:
let
    filepath = fnGetParameter("FullFileName"),
    Source = Excel.Workbook(File.Contents(filepath), null, true),
    #"Filtered Rows" = Table.SelectRows(Source, each not Text.StartsWith([Name], "Required")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Item", "Kind", "Hidden"}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14"}, {"Data.Column1", "Data.Column2", "Data.Column3", "Data.Column4", "Data.Column5", "Data.Column6", "Data.Column7", "Data.Column8", "Data.Column9", "Data.Column10", "Data.Column11", "Data.Column12", "Data.Column13", "Data.Column14"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Data", [PromoteAllScalars=true]),
    #"Renamed Columns City" = Table.RenameColumns(#"Promoted Headers",{{List.First(Table.ColumnNames(#"Promoted Headers")), "City"}}),
    #"Filtered Rows1" = Table.SelectRows(#"Renamed Columns City", each ([Age] <> "Age")),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Filtered Rows1", {"Joining Date", "Target", "Age", "Name", "City"}, "Attribute", "Value"),
    #"Removed Other Columns" = Table.SelectColumns(#"Unpivoted Other Columns",{"Attribute", "Value"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Attribute", type date}, {"Value", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Count", each 1),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Count", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Value]), "Value", "Count", List.Sum),
    #"Renamed Columns Date" = Table.RenameColumns(#"Pivoted Column",{{"Attribute", "Date"}}),
    #"Replaced Value nulls" = Table.ReplaceValue(#"Renamed Columns Date",null,0,Replacer.ReplaceValue,Table.ColumnNames(#"Renamed Columns Date")),
    #"Reordered Columns" = Table.ReorderColumns(#"Replaced Value nulls",{"Date", "P", "A", "CL", "PL", "WFH"})
in
    #"Reordered Columns"

11) In the white Name box on the right hand side, give it a meaningful name
12) Home > Close and Load to Select Table and then either leave it as New Worksheet or select Existing Worksheet and give it a location.

Let me know what you decide and if this works for you.
 
Upvote 0
Hi Alex,

Thanks for your reply.
The source of the data is different regions sending the xlsx files at the end of every month.
The client specifically asked for VBA script for this.


Regards
 
Upvote 0

Forum statistics

Threads
1,215,753
Messages
6,126,675
Members
449,327
Latest member
John4520

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