How to Automate Excel Reports Using Macros (3 Easy Ways)

Get FREE Advanced Excel Exercises with Solutions!

In this article, I’ll show you how you can automate your Excel reports in the offices using Macros.


Automate Excel Reports Using Macros (Quick View)

Sub Profit_Loss_Report()

Data_Sheet = "Sheet1"
Output_Sheet = "Profit or Loss Report"
Cost_Price_Column = 2
Selling_Price_Column = 3

Total_Columns = Worksheets(Data_Sheet).UsedRange.Columns.Count

Sheets.Add.Name = Output_Sheet
Worksheets(Data_Sheet).UsedRange.Copy
Worksheets(Output_Sheet).Activate
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
ActiveSheet.Cells(1, Total_Columns + 1) = "Profit / Loss"
For i = 2 To ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.UsedRange.Cells(i, Total_Columns + 1) = ActiveSheet.UsedRange.Cells(i, Selling_Price_Column) - ActiveSheet.UsedRange.Cells(i, Cost_Price_Column)
Next i

Application.CutCopyMode = False

End Sub

VBA Code to Automate Excel Reports Using Macros


Without further delay, we’ll start our proceedings here. But before starting, I would like to remind you of a few things. You can accomplish a bunch of things using Excel Macros. Regarding automating your business reports, from the balance sheet, and income statement, to the closing journal, you can prepare everything in Excel using Macros.

But I’ll not go so deep here. In fact, one single article is not also enough to cover everything. Today I’ll show you how to generate a simple profit or loss report using Excel Macros.

Here we’ve got a worksheet called Sheet1 with a data set that contains the buying prices and selling prices of some products of a company.

Data Set to Automate Excel Reports Using Macros

First of all, we’ll generate a profit/loss report from this data set.

Then we’ll generate a report of profit/loss percentage.

Finally, we’ll generate a report showing the total profit or loss and its percentage.


1. Automating Profit/Loss Reports Using Excel Macros

To generate the profit/loss report, we’ll use the following VBA Code.

â§­ VBA Code:

Sub Profit_Loss_Report()

Data_Sheet = "Sheet1"
Output_Sheet = "Profit or Loss Report"
Cost_Price_Column = 2
Selling_Price_Column = 3

Total_Columns = Worksheets(Data_Sheet).UsedRange.Columns.Count

Sheets.Add.Name = Output_Sheet
Worksheets(Data_Sheet).UsedRange.Copy
Worksheets(Output_Sheet).Activate
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
ActiveSheet.Cells(1, Total_Columns + 1) = "Profit / Loss"

For i = 2 To ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.UsedRange.Cells(i, Total_Columns + 1) = ActiveSheet.UsedRange.Cells(i, Selling_Price_Column) - ActiveSheet.UsedRange.Cells(i, Cost_Price_Column)
Next i

Application.CutCopyMode = False

End Sub

VBA Code to Automate Excel Reports Using Macros

â§­ Output:

Run this code. It’ll generate a new worksheet called Profit or Loss Report and calculate the profit or loss of each product there.

â§­ Notes:

The first 4 lines of the code contain the inputs to the code (Data Sheet Name, Output Sheet Name, Cost Price Column, and Selling Price Column). Don’t forget to change them according to your needs.

Code Inputs to Automate Excel Reports Using Macros

Read More: How to Make Monthly Report in Excel (with Quick Steps)


Similar Articles


2. Using Excel Macros to Automate Profit/Loss Percentage Reports

Next, we’ll generate a report containing the percentages of the profits/losses and the numerical values.

The VBA code will be:

â§­ VBA Code:

Sub Profit_Loss_Percentage_Report()

Data_Sheet = "Sheet1"
Output_Sheet = "Profit or Loss Percentage"
Cost_Price_Column = 2
Selling_Price_Column = 3

Total_Columns = Worksheets(Data_Sheet).UsedRange.Columns.Count

Sheets.Add.Name = Output_Sheet
Worksheets(Data_Sheet).UsedRange.Copy
Worksheets(Output_Sheet).Activate
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
ActiveSheet.Cells(1, Total_Columns + 1) = "Profit / Loss"
ActiveSheet.Cells(1, Total_Columns + 2) = "Percentage"

For i = 2 To ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.UsedRange.Cells(i, Total_Columns + 1) = ActiveSheet.UsedRange.Cells(i, Selling_Price_Column) - ActiveSheet.UsedRange.Cells(i, Cost_Price_Column)
    ActiveSheet.UsedRange.Cells(i, Total_Columns + 2) = Format((ActiveSheet.UsedRange.Cells(i, Total_Columns + 1) / ActiveSheet.UsedRange.Cells(i, Cost_Price_Column)), "#.00%")
Next i

Application.CutCopyMode = False

End Sub

VBA Code to Automate Excel Reports Using Macros

â§­ Output:

Run this code. It’ll generate a new worksheet called Profit or Loss Percentage and first calculate the profit or loss of each product, then the percentage.

â§­ Notes:

Again, the first 4 lines of the code contain the inputs to the code (Data Sheet Name, Output Sheet Name, Cost Price Column, and Selling Price Column). Don’t forget to change them according to your needs.

Code Inputs to Automate Excel Reports Using Macros

Read More: How to Generate Reports from Excel Data (2 Easy Methods)


3. Automating a Report Containing the Total Profit/Loss and the Percentage

Finally, we’ll generate a report that’ll contain the profit/loss of each product, the percentage, and the total profit/loss.

We’ll use the following VBA code for this purpose.

â§­ VBA Code:

Sub Total_Profit_Loss_Report()

Data_Sheet = "Sheet1"
Output_Sheet = "Total Profit or Loss"
Cost_Price_Column = 2
Selling_Price_Column = 3

Total_Rows = Worksheets(Data_Sheet).UsedRange.Rows.Count
Total_Columns = Worksheets(Data_Sheet).UsedRange.Columns.Count

Sheets.Add.Name = Output_Sheet
Worksheets(Data_Sheet).UsedRange.Copy
Worksheets(Output_Sheet).Activate
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
ActiveSheet.Cells(1, Total_Columns + 1) = "Profit / Loss"
ActiveSheet.Cells(1, Total_Columns + 2) = "Percentage"

Total_Cost_Price = 0
Total_Selling_Price = 0

For i = 2 To ActiveSheet.UsedRange.Rows.Count
    Total_Cost_Price = Total_Cost_Price + ActiveSheet.UsedRange.Cells(i, Cost_Price_Column)
    Total_Selling_Price = Total_Selling_Price + ActiveSheet.UsedRange.Cells(i, Selling_Price_Column)
Next i

ActiveSheet.UsedRange.Cells(Total_Rows + 1, 1) = "Total"
ActiveSheet.UsedRange.Cells(Total_Rows + 1, Cost_Price_Column) = Total_Cost_Price
ActiveSheet.UsedRange.Cells(Total_Rows + 1, Selling_Price_Column) = Total_Selling_Price

For i = 2 To ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.UsedRange.Cells(i, Total_Columns + 1) = ActiveSheet.UsedRange.Cells(i, Selling_Price_Column) - ActiveSheet.UsedRange.Cells(i, Cost_Price_Column)
    ActiveSheet.UsedRange.Cells(i, Total_Columns + 2) = Format((ActiveSheet.UsedRange.Cells(i, Total_Columns + 1) / ActiveSheet.UsedRange.Cells(i, Cost_Price_Column)), "#.00%")
Next i

Application.CutCopyMode = False

End Sub

â§­ Output:

Run this code. It’ll generate a new worksheet called Total Profit or Loss. Then it’ll calculate the profit/loss of each product, its percentage, the total profit/loss, and the percentage.

â§­ Notes:

Again, the first 4 lines of the code contain the inputs to the code (Data Sheet Name, Output Sheet Name, Cost Price Column, and Selling Price Column). Don’t forget to change them according to your needs.

Read More: How to Create a Summary Report in Excel (2 Easy Methods)


Things to Remember

Once you run any of the above 3 codes, a new worksheet is created with the respective name. If you want to run the code again, don’t forget to delete this worksheet. Otherwise, Excel can’t generate two worksheets of the same name and will show an error.


Download Practice Workbook

Download this practice workbook to exercise while you are reading this article.


Conclusion

So, these are the ways to automate some simple reports using Excel Macros. Do you have any questions? Feel free to ask us. And don’t forget to visit our site ExcelDemy for more posts and updates.


Related Articles

Rifat Hassan
Rifat Hassan

Hello! Welcome to my profile. Here I will be posting articles related to Microsoft Excel. I am a passionate Electrical Engineer holding a Bachelor’s degree in Electrical and Electronic Engineering from the Bangladesh University of Engineering and Technology. Besides academic studies, I always love to keep pace with the revolution in technology that the world is rushing towards day by day. I am diligent, career-oriented, and ready to cherish knowledge throughout my life.

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo