Consolidate in Excel

Consolidate in Excel

Consolidate:

To summarize and report results from separate worksheets, you can consolidate data from each separate worksheet into a master worksheet. In this article I am using two sheets having data and will consolidate in master sheet as given in steps below:

  • Select the upper-left cell of the area where you want the consolidated data to appear
  • On the Ribbon, Choose Data > Consolidate to view the Consolidate dialog
  • In the Function box, click the summary function that you want Excel to use to consolidate the data

Consolidate Dialog:

Click on range selector to navigate first data source as shown below:

Click Add button and again click on range selector to navigate next sheet (repeat the process and select data sources from various sheets by hitting Add button):

There are following options which can be enabled during consolidation process:

  1. Top row: it place the headers if selected
  2. Left column: It brings left column (column 1) if selected
  3. Create links to source data: It creates group at the row level which can be toggled with the help of Group (+) or Group (-) to see the links of the data

Finally I selected all three options above and output looks like as below:

C# code example:

private void btnConsolidate_Click(object sender, RibbonControlEventArgs e)
{
	Globals.ThisAddIn.Application.ReferenceStyle=excel.XlReferenceStyle.xlR1C1;
	//Retain selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;

	//get active workbook reference
	excel.Workbook oWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook;

	//Get first worksheet
	excel.Worksheet oFirstSheet=oWorkbook.Worksheets["Sheet7"];
	//Obtain range
	excel.Range oFirstRange = oFirstSheet.Range["A1:C19"];

	//Get second worksheet
	excel.Worksheet oSecondSheet = oWorkbook.Worksheets["Sheet8"];
	//Obtain range
	excel.Range oSecondRange = oSecondSheet.Range["A1:C20"];


   //Build R1C1 address
	string firstSource = oFirstSheet.Name + "!" +
						 oFirstRange.Address[false,false,excel.XlReferenceStyle.xlR1C1,false];

	//Build R1C1 address
	string secondSource = oSecondSheet.Name + "!" +
						  oSecondRange.Address[false, false, excel.XlReferenceStyle.xlR1C1, false];

	//Bind into array
	string[] oListRange= { firstSource,secondSource};
	

	//Consolidate
	oRange.Consolidate(oListRange,excel.XlConsolidationFunction.xlSum, true, true, true);
}

Note: Consolidate method needs source range address in R1C1 style else code would be broken. Also in my example I am consolidating data within same workbook hence passing only Sheet Name and Range Address in R1C1 format, if you want to consolidate data from different files you need to supply complete path associated by Sheet name and range address.

VB.Net code example:

Private Sub btnConsolidateData_Click(sender As Object, e As RibbonControlEventArgs) Handles btnConsolidateData.Click

	Globals.ThisAddIn.Application.ReferenceStyle=excel.XlReferenceStyle.xlR1C1
	
	'Retain selection
	Dim oRange As excel.Range
	oRange=Globals.ThisAddIn.Application.Selection

	'get active workbook reference
	dim oWorkbook as excel.Workbook
	oWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook

	'Get first worksheet
	dim oFirstSheet As excel.Worksheet
	oFirstSheet =oWorkbook.Worksheets("Sheet7")
	'Obtain range
	dim oFirstRange as excel.Range
	oFirstRange = oFirstSheet.Range("A1:C19")

	'Get second worksheet
	dim oSecondSheet As excel.Worksheet
	oSecondSheet = oWorkbook.Worksheets("Sheet8")
	'Obtain range
	dim oSecondRange As excel.Range
	oSecondRange = oSecondSheet.Range("A1:C20")


	'Build R1C1 address
	dim firstSource as string
	firstSource = oFirstSheet.Name + "!" +
							 oFirstRange.Address(false,false,excel.XlReferenceStyle.xlR1C1,false)

	'Build R1C1 address
	dim secondSource as string
	secondSource = oSecondSheet.Name + "!" +
							  oSecondRange.Address(false, false, excel.XlReferenceStyle.xlR1C1, false)

	'Bind into array
	dim [oListRange]() As String = { firstSource,secondSource}
		

	'Consolidate
	 oRange.Consolidate([oListRange],excel.XlConsolidationFunction.xlSum, true, true, true)
End Sub

VBA code example:

Public Sub ConsolidateData()
    Selection.Consolidate Sources:=Array( _
        "Sheet7!R1C1:R19C3" _
        , _
        "Sheet8!R1C1:R19C3" _
        ), Function:=xlSum, TopRow:=True, LeftColumn:=True, CreateLinks:=True
End Sub

Next >> Forecast data in Excel using scenarios

Leave a Reply

Your email address will not be published.