Crazy Data Aggregation Question


New Member
Dec 4, 2009
Hi All,

You have helped me out tons in the past with some of my Excel mindbenders and here is another one if anyone can help!

I have 100+ worksheets spread across about nine workbooks with data that needs aggregated together. Each workbook has identical fields between its own worksheets, but the data between workbooks have slightly different field names and some have more field names than others.

I need to take each workbook and aggregate every worksheet in that book into one dataset. Unfortunately the data is not a flat dataset in every worksheet, however they are formated the same. For example, some worksheets have one or two blank rows before the column heading row for that sheet's dataset.

Can anyone think of a macro that would look at each sheet of a book, take the dataset minus the header row, and combine the data from every sheet in that book into one block of data in a new sheet named CompleteData or something like that? I also need to pull the sheet name into the new dataset.

Here is an example of the data that I need to combine:

Any ideas?



MrExcel MVP
Sep 2, 2009
Sub Consolidate_Sheets()

    Dim ws As Worksheet
    Dim wsFinal As Worksheet
    Dim Lastcol As Long
    Dim Nextrow As Long
    Dim Firstrow As Long
    Dim Lastrow As Long
    On Error Resume Next
        Set wsFinal = Sheets("Final Data")
    On Error GoTo 0
    If Not wsFinal Is Nothing Then
        MsgBox "Sheet ""Final Data"" already exists. ", , "Final Data Exists"
        Exit Sub
    End If
    Application.ScreenUpdating = False
    Set wsFinal = ActiveWorkbook.Sheets.Add(After:=Sheets(Sheets.Count))
    wsFinal.Name = "Final Data"
    If Sheets(1).Range("A1").Value <> "" Then
        Sheets(1).Range("A1").EntireRow.Copy wsFinal.Range("1:1")
        Sheets(1).Range("A1").End(xlDown).EntireRow.Copy wsFinal.Range("1:1")
    End If
    Lastcol = wsFinal.Cells(1, wsFinal.Columns.Count).End(xlToLeft).Column + 1
    wsFinal.Cells(1, Lastcol).Value = "Worksheet"
    For Each ws In ActiveWorkbook.Sheets
        If ws.Name <> wsFinal.Name Then
            Nextrow = wsFinal.Range("A" & Rows.Count).End(xlUp).Row + 1
            If ws.Range("A1").Value <> "" Then
                Firstrow = 2
                Firstrow = ws.Range("A1").End(xlDown).Row + 1
            End If
            Lastrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
            With ws.Range(ws.Cells(Firstrow, 1), ws.Cells(Lastrow, Lastcol - 1))
                wsFinal.Cells(Nextrow, 1).Resize(.Rows.Count, .Columns.Count).Value = .Value
                wsFinal.Cells(Nextrow, Lastcol).Resize(.Rows.Count).Value = ws.Name
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
Last edited:

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...