1. Article / Tutorial
  2. Outline

Data summarization is a crucial process in data analysis, providing a snapshot of key metrics that can guide decision-making. This macro automates the generation of a summary report from a selected data range, calculating totals, averages, and counts, which are fundamental for a quick analysis of various types of numerical data.

Description

The Data Summarization macro performs the following operations on a specified range of data:

  • Totals: Calculates the sum of numerical values in a column.
  • Averages: Computes the average (mean) value of data in a column.
  • Count: Counts the number of entries in a given data range.

Use Case

This macro is invaluable for anyone needing to perform a quick analysis of datasets, such as sales figures, expense reports, or any other numerical data. By providing immediate insights into totals, averages, and counts, it helps in evaluating performance, budgeting, and planning.

Example Spreadsheet Data

Consider a spreadsheet containing monthly sales data for different products:

Product January February March April
Product A 150 200 180 190
Product B 90 120 110 100
Product C 200 210 205 220

Objective: Generate a summary report that includes the total, average sales, and the count of months recorded for each product.

Code Snippet

Microsoft Excel (VBA):

Sub GenerateSummaryReport()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' Create headers for the summary
    With ws
        .Cells(1, 6).Value = "Product"
        .Cells(1, 7).Value = "Total Sales"
        .Cells(1, 8).Value = "Average Sales"
        .Cells(1, 9).Value = "Months Counted"
    End With

    ' Calculate summary data
    Dim i As Long
    For i = 2 To lastRow
        ws.Cells(i, 6).Value = ws.Cells(i, 1).Value
        ws.Cells(i, 7).Value = Application.WorksheetFunction.Sum(ws.Range(ws.Cells(i, 2), ws.Cells(i, 5)))
        ws.Cells(i, 8).Value = Application.WorksheetFunction.Average(ws.Range(ws.Cells(i, 2), ws.Cells(i, 5)))
        ws.Cells(i, 9).Value = Application.WorksheetFunction.CountA(ws.Range(ws.Cells(i, 2), ws.Cells(i, 5)))
    Next i

    MsgBox "Summary report generated successfully."
End Sub

Google Sheets (Google Apps Script):

function generateSummaryReport() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var dataRange = sheet.getRange("A2:E" + sheet.getLastRow());
  var data = dataRange.getValues();
  
  // Set up summary headers
  sheet.getRange("G1").setValue("Product");
  sheet.getRange("H1").setValue("Total Sales");
  sheet.getRange("I1").setValue("Average Sales");
  sheet.getRange("J1").setValue("Months Counted");

  // Calculate summary data
  for (var i = 0; i < data.length; i++) {
    var row = data[i];
    sheet.getRange(i + 2, 7).setValue(row[0]); // Product
    sheet.getRange(i + 2, 8).setValue(row.slice(1).reduce(function(a, b) { return a + b; }, 0)); // Total Sales
    sheet.getRange(i + 2, 9).setValue(row.slice(1).reduce(function(a, b) { return a + b; }, 0) / row.slice(1).length); // Average Sales
    sheet.getRange(i + 2, 10).setValue(row.slice(1).filter(String).length); // Months Counted
  }

  SpreadsheetApp.getUi().alert('Summary report generated successfully.');
}

After executing this macro, a summary report will be created adjacent to the original data, providing a concise overview of the total sales, average sales, and the count of months recorded for each product:

Product Total Sales Average Sales Months Counted
Product A 720 180 4
Product B 420 105 4
Product C 835 208.75 4

This summary not only simplifies the analysis process but also equips decision-makers with essential metrics to assess performance and trends over time efficiently.

Macro 4: Data Summarization

  • Description: Generates a summary report from a selected range of data, including totals, averages, and count.
  • Use Case: Quick analysis of sales, expenses, or any numerical data.
  • Code Snippet: VBA/Google Apps Script example.

All text and images are Copyright © Office Spreadsheets

User privacy is important and is detailed in our Privacy Policy.

See our Cookie Policy for how we use cookies and user options.

See also Office Spreadsheets Terms of Service.