How to Generate Reports Using Macros in Excel (with Easy Steps)

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 reports in excel using macros


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:

generate reports in excel using macros

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.

generate reports in excel using macros

A window named Microsoft Visual Basic for Applications – Generate Report opens.

  • Go to Insert → Module.

generate reports in excel using macros

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

generate reports in excel using macros

  • Run the code by clicking Run → Run Sub/UserForm.

A report is generated.

generate reports in excel using macros

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

Get FREE Advanced Excel Exercises with Solutions!
Md. Abdur Rahim Rasel
Md. Abdur Rahim Rasel

MD. ABDUR RAHIM is a marine engineer proficient in Excel and passionate about programming with VBA. He views programming as an efficient means to save time while managing data, handling files, and engaging with the internet. His interests extend to Rhino3D, Maxsurf C++, AutoCAD, Deep Neural Networks, and Machine Learning, reflecting his versatile skill set. He earned a B.Sc in Naval Architecture & Marine Engineering from BUET, and now he has become a content developer, creating technical content... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo