Consolidating sheets in an Excel workbook

Lets say you have an Excel workbook consisting of tabs, all in the same format, that various people completed. You want to copy them all to a consolidated sheet from time to time so you can do pivot tables etc.

I’ve talked about abstracting Excel data on this blog several times, and also introduced you to some tools to make that available. Using cDataSet, you can solve this consolidation problem in just  a few lines of code.

First make a list of the sheets you want to concatenate. In this case I’ve listed out them in a tab called ‘inputs’, in a column called ‘names’, and create a new sheet called ‘consolidated’.

Executing this code will then take each of those sheets mentioned in the names column and consolidate them.

Sub consolidate()
    Dim dst As cDataSet, ds As cDataSet, n As Long, dc As cCell
    Set dst = New cDataSet
    dst.populateData wholeSheet("inputs"), , , , , , True
    n = 0
    For Each dc In dst.Column("names").Rows
        Set ds = New cDataSet
        ds.populateData wholeSheet(dc.toString), , dc.toString, , , , True
        n = n + ds.bigCommit(wholeSheet("consolidated").Offset(n), n = 0, , , , , n = 0)
        Set ds = Nothing
    Next dc   
End Sub

For more usages of data abstraction, please visit ramblings.mcpher.com.

About brucemcp 225 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.