Consolidate Data Using VBA

How to consolidate data automatically using VBA. We have used the DSUM worksheet function and an array to automate the process of consolidation of data from multiple worksheets. The array of employee names is used in the criteria range of the Dsum function which helps in the automation of consolidating the salaries and the perks of the employees. First we get the data from multiple sheets into a new worksheet. In the new worksheet we can easily and quickly sum the relevant data for each employee. Watch the video below:

Watch this video on YouTube.

Here’s the complete VBA to consolidate data from multiple worksheets:

Sub consolidateDataUsingArray()
Dim empNames As Variant
Dim lastrow As Long
lastrow = Sheets(“Sheet2”).Range(“A” & Rows.Count).End(xlUp).Row

Dim rng As Range
Set rng = Sheets(“sheet5”).Range(“A2:A” & lastrow)
empNames = rng

For i = LBound(empNames) To UBound(empNames)
Range(“G2”) = empNames(i, 1)
Range(“L1”) = “employee Name”
Range(“M1”) = “Total Salary Paid”
Range(“N1”) = “Total Perks Paid”
j = i + 1
Cells(j, 12) = empNames(i, 1)
Cells(j, 13) = Application.WorksheetFunction.DSum(Range(“A1:C37”), 2, Range(“G1:I2”))
Cells(j, 14) = Application.WorksheetFunction.DSum(Range(“A1:C37”), 3, Range(“G1:I2”))

Next i

Columns(“A:I”).EntireColumn.Hidden = True

End Sub

Sub mergeData()
Dim j As Integer

On Error Resume Next
Worksheets.Add ‘ add a sheet
Sheets(1).Name = “MergedData”

‘ copy headers
Selection.Copy Destination:=Sheets(1).Range(“A1”)

‘ loop through sheets
For j = 2 To Sheets.Count

‘ header cells are not selected
Selection.Offset(1, 0).Resize(Selection.Rows.Count – 1).Select

‘ copy cells selected in the new sheet on the next blank row
Selection.Copy Destination:=Sheets(1).Range(“A65536”).End(xlUp)(2)


End Sub

Sub unhidecolumns()
Columns(“A:I”).EntireColumn.Hidden = False
End Sub

Sample file for download and practice:

Published by

Dinesh Kumar Takyar

Welcome to! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: For a structured Excel VBA training course online you can visit:

One thought on “Consolidate Data Using VBA”

  1. Hi sir,

    Thank you for sharing the knowledge. Your videos are really helpful. I have 2 questions
    1. How can we save a file from internet explorer when save option pops up using vba.
    2. What will be the vba code to reply on any selected outlook email with predefined template.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.