Excel VBA: create a report from a list automatically

The raw data sits in a long list, but what people need is a report they can print or pass on. A macro adds a sheet for it, writes the headings, adds up each group and formats everything the same way. Every month at the push of a button, without touching the raw data.

In short: Set report = Worksheets.Add(After:=data) adds a sheet for the report and keeps it in a variable. You then write headings, totals and formats onto it, for example report.Range("B3").NumberFormat = "#,##0.00". Totals per group are collected in a Dictionary first.

What you need to know: worksheets, formatting cells, the Dictionary and error handling. The data often comes from a summary sheet like the one in combine sheets.

Try it right now

The Data sheet lists sales per region, followed by a Notes sheet. The macro puts the report between the two and fills in the key figures.

Task The macro SalesReport should add a new sheet Report directly after the Data sheet. On it: "Sales report" in bold in A1, "Total sales" in A3 and the sum of all sales on Data in B3 with the number format #,##0.00, "Number of orders" in A4 and the number of data rows in B4.

Data

ABCD
1RegionSales
2North1,250.00
3South980.50
4North2,300.00
5West412.00
6South760.00
7East1,900.00
8West540.00
9North128.40

Notes

ABCD
1Export from the sales system, March 2026
2
3
4
5

How a report macro is built

Nearly every report macro follows the same three steps: read and calculate, add the sheet, write and format. Keeping the steps apart makes mistakes easier to find.

Sub SalesReport()
    Dim data As Worksheet, report As Worksheet
    Dim lastRow As Long
    Set data = Worksheets("Data")
    lastRow = data.Cells(data.Rows.Count, "A").End(xlUp).Row

    Set report = Worksheets.Add(After:=data)
    With report
        .Name = "Report"
        .Range("A1").Value = "Sales report"
        .Range("A1").Font.Bold = True
        .Range("A3").Value = "Total sales"
        .Range("B3").Value = WorksheetFunction.Sum(data.Range("B2:B" & lastRow))
        .Range("B3").NumberFormat = "#,##0.00"
    End With
End Sub
PartWhat it does
Worksheets.Add(After:=data)A new sheet right after Data. With parentheses, Add returns the new sheet, and Set stores it in the variable.
With report … End WithEvery line starting with a dot refers to the report sheet. Shorter, and safe even when another sheet is active.
.Name = "Report"Names the sheet. Without it, the sheet keeps Excel's default name, such as Sheet3.
WorksheetFunction.Sum(…)Works out the sum once and writes the value. If the cell should keep calculating, write a formula with .Formula instead.
.NumberFormat = "#,##0.00"Thousands separator and two decimals. No currency symbol, so it suits dollars and pounds alike.

Totals per group with a Dictionary

A report rarely stops at one grand total. Usually it wants a row per region, cost center or month. A Dictionary keeps a running total per key in a single pass:

For i = 2 To lastRow
    dict(data.Cells(i, 1).Value) = dict(data.Cells(i, 1).Value) + data.Cells(i, 2).Value
Next i

If the key does not exist yet, reading it creates it empty, and empty plus a number is the number. Afterwards, For Each region In dict.Keys walks through the groups and you write one row each. Keys come back in the order they were added. If the report should be sorted by sales, sort the finished range on the report sheet with Sort.

Another exercise: sales by region

Now with one row per region, a bold total row and the same number format throughout. Check compares bold and number formats too.

Task The macro SalesByRegion should add a sheet Report after Data. Header in A1:B1: "Region" and "Sales" in bold. Below it one row per region with the sum of its sales (regions in the order they first appear on Data), then a row "Total" with the sum of all sales, also bold in A and B. Every amount in column B gets the number format #,##0.00.

ABCD
1RegionSales
2North1,250.00
3South980.50
4North2,300.00
5West412.00
6South760.00
7East1,900.00
8West540.00
9North128.40

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

One sheet per category

Sometimes every department or category should get a sheet of its own, for example to hand it on. Then for each row you need to know whether that sheet exists yet. Worksheets("Tech") raises a run-time error when there is no sheet by that name, and you can catch that error on purpose:

Set ws = Nothing
On Error Resume Next
Set ws = Worksheets(cat)
On Error GoTo 0
If ws Is Nothing Then
    Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    ws.Name = cat
    data.Range("A1:C1").Copy Destination:=ws.Range("A1")
End If

Error handling is switched off for exactly one line and back on straight after, so real errors still show up.

Task The macro SheetPerCategory should create one sheet per category on Data, named like the category and added at the end, in the order the categories first appear. Each sheet gets the header from Data (A1:C1) and below it every row of that category, in the original order.

ABCD
1CategoryItemAmount
2OfficeCopy paper118.00
3Tech27-inch monitor356.00
4FurnitureOffice chair289.00
5OfficeRing binder61.25
6TechHeadset89.90
7OfficeBlack toner147.00
8FurnitureMobile pedestal199.00

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

In real Excel

A report macro often runs every month. On the second run the Report sheet already exists, and .Name = "Report" fails because sheet names must be unique. Either delete the old sheet first (after asking, see the next lesson) or add the month to the name, for example "Report " & Format(Date, "yyyy-mm"). Sheet names can be at most 31 characters long and cannot contain : \ / ? * [ ]. For a print-ready report, Columns("A:B").AutoFit fits the column widths, and a border around the total row helps too.

Common mistakes

The report lands in the wrong place

With Before:=data, or with no argument at all, the sheet ends up before Data. The first exercise then says "The sheets are not right. Expected: Data, Report, Notes. Yours: Report, Data, Notes."

The number format is close but not the same

"0.00" shows two decimals but no thousands separator. The exercise reports "The number format is not right in Report!B3 (NumberFormat)." Type the format exactly as the task gives it.

The total row is not bold

Bold only in A, or forgetting the row, gives "Bold is not right in Report!A6 (Font.Bold)." It is easiest to format whole ranges at the end: .Range("A" & r & ":B" & r).Font.Bold = True.

Forgetting Set ws = Nothing

Without that line, ws still points at the previous sheet. When Set ws = Worksheets(cat) fails, the old value stays, and every row ends up on the first category sheet. The third exercise then says "The sheets are not right. Expected: Data, Office, Tech, Furniture. Yours: Data, Office."

FAQ

Should the report hold values or formulas?

Values when the report records a snapshot that should not change, such as a month-end close. Formulas when the report should update if someone corrects the data. For a formula you would write something like .Range("B3").Formula = "=SUM(Data!B2:B" & lastRow & ")".

Isn't a PivotTable easier?

For totals per group, a PivotTable is often quicker, and needs no macro. A macro pays off when the report needs a fixed layout with your own text, a sheet per department, or checks a PivotTable cannot do. Macros that build PivotTables are not part of the exercises here.

How do I sort the groups?

Write the groups onto the report sheet as shown, then sort the range without the total row, for example .Range("A1:B" & r - 1).Sort Key1:=.Range("B2"), Order1:=xlDescending, Header:=xlYes. Write the total row below it afterwards.

Can the macro save the report as a PDF?

In real Excel, yes, with ExportAsFixedFormat. The exercises here have no file system, so it does not come up.

Up next: enable macros and code safely

Why Excel blocks macros when you open a file, how to allow them for files you trust, and how a macro asks before deleting and makes a backup first.

Enable macros

Write to us

Choose language

Select your preferred language