In this article, we’ll demonstrate a quick, effective method to generate reports in Excel using Macros.
Quick View of the Macro Code
Private Sub GR()
Dim nextrow As Long
Sheets("Generate Report").Select
Sheets("Generate Report").Cells.ClearContents
Range("A1").Value = "Name"
Range("B1").Value = "ID"
Range("C1").Value = "Product"
Range("D1").Value = "Rev earned"
For x = 1 To Sheets.Count - 1
Sheets(x).Range("A2:D50").Copy
nextrow = Sheets("Generate Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
Sheets("Generate Report").Cells(nextrow, 1).PasteSpecial Paste:=xlPasteValues
Next
Cells(1, 5).Select
Application.CutCopyMode = False
Sheets("Generate Report").Range(Cells(1, 1), Cells(nextrow, 4)).Columns.AutoFit
End Sub
Generate a Report Using Macros in Excel: 2 Easy Steps
Suppose we have a large worksheet that contains the information about several sales representatives in two different datasets. We will generate a report using these two datasets.
Here’s an overview of dataset 1:
And here’s an overview of dataset 2:
Step 1 – Open Visual Basic Window
We’ll generate a report by using a simple VBA code, which is written in a VBA Module.
Steps:
- To open a Module, go to Developer → Visual Basic.
A window named Microsoft Visual Basic for Applications – Generate Report opens.
- Go to Insert → Module.
A Module window opens.
Step 2 – Write and Run VBA Macro Code to Generate Reports
Steps:
- In the Module window, enter the following VBA code:
Private Sub GR()
Dim nextrow As Long
Sheets("Generate Report").Select
Sheets("Generate Report").Cells.ClearContents
Range("A1").Value = "Name"
Range("B1").Value = "ID"
Range("C1").Value = "Product"
Range("D1").Value = "Rev earned"
For x = 1 To Sheets.Count - 1
Sheets(x).Range("A2:D50").Copy
nextrow = Sheets("Generate Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
Sheets("Generate Report").Cells(nextrow, 1).PasteSpecial Paste:=xlPasteValues
Next
Cells(1, 5).Select
Application.CutCopyMode = False
Sheets("Generate Report").Range(Cells(1, 1), Cells(nextrow, 4)).Columns.AutoFit
End Sub
- Run the code by clicking Run → Run Sub/UserForm.
A report is generated.
Read More: How to Generate Report in PDF Format Using Excel VBA
Things to Remember
Another way to open the Microsoft Visual Basic for Applications window is by pressing Alt + F11.
If the Developer tab is not visible in your ribbon, enable it by going to File → Option → Customize Ribbon.
Download Practice Workbook
Related Articles
- How to Automate Excel Reports Using Macros
- Create a Report in Excel as a Table
- How to Generate PDF Reports from Excel Data
- How to Generate Reports from Excel Data
- How to Create a Summary Report in Excel
- How to Make Report Card in Excel