0
0
Excelspreadsheet~15 mins

Variables and loops in VBA in Excel - Real Business Scenario

Choose your learning style9 modes available
Scenario Mode
👤 Your Role: You are an Excel analyst working in the finance department.
📋 Request: Your manager wants you to automate the calculation of total sales for each product category using VBA. They want a macro that loops through sales data and sums sales by category.
📊 Data: You have a table with columns: Product Category, Product Name, and Sales Amount. The data has multiple rows for different products and categories.
🎯 Deliverable: Create a VBA macro that uses variables and loops to calculate total sales per product category and outputs the results in a new summary table.
Progress0 / 9 steps
Sample Data
Product CategoryProduct NameSales Amount
ElectronicsSmartphone500
ElectronicsLaptop800
FurnitureChair150
FurnitureTable300
ClothingT-shirt50
ClothingJeans100
ElectronicsHeadphones200
FurnitureSofa700
1
Step 1: Open the VBA editor by pressing Alt + F11 in Excel.
Expected Result
VBA editor window opens.
2
Step 2: Insert a new module: In the VBA editor, click Insert > Module.
Expected Result
A new blank module appears to write code.
3
Step 3: Declare variables to store the last row, loop counter, category name, sales total, and a dictionary to hold totals.
Dim lastRow As Long Dim i As Long Dim category As String Dim salesTotal As Double Dim salesDict As Object Set salesDict = CreateObject("Scripting.Dictionary")
Expected Result
Variables declared and dictionary object created.
4
Step 4: Find the last row of data in column A (Product Category) to know how many rows to loop through.
lastRow = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, 1).End(xlUp).Row
Expected Result
lastRow is set to 9 (the last data row).
5
Step 5: Create a loop from row 2 to lastRow to read each product category and sales amount.
For i = 2 To lastRow category = Sheets("Sheet1").Cells(i, 1).Value salesTotal = Sheets("Sheet1").Cells(i, 3).Value
Expected Result
Loop starts reading each row's category and sales.
6
Step 6: Inside the loop, check if the category exists in the dictionary. If yes, add sales amount; if no, add new key with sales amount.
If salesDict.Exists(category) Then salesDict(category) = salesDict(category) + salesTotal Else salesDict.Add category, salesTotal End If Next i
Expected Result
Dictionary accumulates total sales per category.
7
Step 7: Output the results starting at cell E1 with headers 'Category' and 'Total Sales'.
Sheets("Sheet1").Cells(1, 5).Value = "Category" Sheets("Sheet1").Cells(1, 6).Value = "Total Sales"
Expected Result
Headers appear in cells E1 and F1.
8
Step 8: Use a loop to write each category and its total sales from the dictionary to the sheet starting at row 2.
Dim rowOut As Long rowOut = 2 For Each category In salesDict.Keys Sheets("Sheet1").Cells(rowOut, 5).Value = category Sheets("Sheet1").Cells(rowOut, 6).Value = salesDict(category) rowOut = rowOut + 1 Next category
Expected Result
Summary table with categories and total sales appears in columns E and F.
9
Step 9: Run the macro to see the total sales per product category.
Expected Result
Summary table shows Electronics: 1500, Furniture: 1150, Clothing: 150.
Final Result
Category     | Total Sales
--------------------------
Electronics  | 1500
Furniture    | 1150
Clothing    | 150
Electronics category has the highest total sales of 1500.
Furniture category follows with total sales of 1150.
Clothing category has the lowest total sales of 150.
Bonus Challenge

Modify the macro to also calculate and display the average sales per product category.

Show Hint
Use another dictionary to count the number of products per category, then divide total sales by count.