- BizOps Analytics
- Posts
- Automating Data Cleanup and Highlighting Key Insights
Automating Data Cleanup and Highlighting Key Insights
An Interesting Excel Macro Trick
Unlock AI-powered productivity
HoneyBook is how independent businesses attract leads, manage clients, book meetings, sign contracts, and get paid.
Plus, HoneyBook’s AI tools summarize project details, generate email drafts, take meeting notes, predict high-value leads, and more.
Think of HoneyBook as your behind-the-scenes business partner—here to handle the admin work you need to do, so you can focus on the creative work you want to do.
Introduction
Excel macros, powered by VBA (Visual Basic for Applications), can transform repetitive tasks into automated workflows, saving time and reducing errors. One particularly useful and interesting trick is creating a macro that automates data cleanup and highlights key insights in a dataset such as identifying and formatting outliers or duplicates while summarizing key metrics. This is especially handy for professionals like data analysts who deal with messy datasets daily. Let’s dive into this trick with a practical example.
10x Your Outbound With Our AI BDR
Your BDR team is wasting time on things AI can automate. Artisan’s AI BDR Ava automates lead research, multi-channel outreach and follow-ups on behalf of your team.
Ava operates within the Artisan platform, which consolidates every tool you need for outbound:
300M+ High-Quality B2B Prospects, including E-Commerce and Local Business Leads
Automated Lead Enrichment With 10+ Data Sources
Full Email Deliverability Management
Multi-Channel Outreach Across Email & LinkedIn
Human-Level Personalization
The Scenario
Imagine you’re a sales analyst working with a monthly sales report in Excel. The dataset contains sales data for 50 products, including columns for Product Name, Sales Amount, Region, and Date. However, the data is messy: there are blank rows, duplicate entries, and you need to highlight products with sales above a certain threshold (e.g., $10,000) for a quick review. Additionally, you want a summary of total sales and the top-performing region. Doing this manually would take hours, but a macro can handle it in seconds.
The Macro: Cleanup, Highlight, and Summarize
Below is a VBA macro that automates this process. It removes blank rows, deletes duplicates, highlights high sales in green, and creates a summary at the bottom of the sheet.
Step 1: Set Up Your Excel Sheet
Your data is in Sheet1, starting from cell A1 with headers: Product Name (A), Sales Amount (B), Region (C), and Date (D).
The data spans rows 2 to 51 (50 products).
Step 2: Open the VBA Editor
Press Alt + F11 to open the VBA Editor.
Go to Insert > Module and paste the following code.
The VBA Code
vba
Sub CleanHighlightSummarize()
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long, i As Long
Dim totalSales As Double
Dim topRegion As String
Dim regionSales As Object
Dim maxSales As Double
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
' Find the last row of data
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Set the data range (excluding headers)
Set rng = ws.Range("A2:D" & lastRow)
' Step 1: Remove blank rows
For i = lastRow To 2 Step -1
If Application.CountA(ws.Rows(i)) = 0 Then
ws.Rows(i).Delete
End If
Next i
' Update last row after deleting blanks
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Step 2: Remove duplicates based on Product Name (Column A)
ws.Range("A1:D" & lastRow).RemoveDuplicates Columns:=1, Header:=xlYes
' Update last row after removing duplicates
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Step 3: Highlight sales > $10,000 in green
For i = 2 To lastRow
If ws.Cells(i, 2).Value > 10000 Then
ws.Rows(i).Interior.Color = vbGreen
End If
Next i
' Step 4: Calculate total sales and find top region
totalSales = Application.Sum(ws.Range("B2:B" & lastRow))
' Use a dictionary to track sales by region
Set regionSales = CreateObject("Scripting.Dictionary")
For i = 2 To lastRow
Dim region As String
region = ws.Cells(i, 3).Value
If Not regionSales.exists(region) Then
regionSales.Add region, ws.Cells(i, 2).Value
Else
regionSales(region) = regionSales(region) + ws.Cells(i, 2).Value
End If
Next i
' Find the top region
maxSales = 0
For Each Key In regionSales.keys
If regionSales(Key) > maxSales Then
maxSales = regionSales(Key)
topRegion = Key
End If
Next Key
' Step 5: Add a summary below the data
ws.Cells(lastRow + 2, 1).Value = "Summary"
ws.Cells(lastRow + 3, 1).Value = "Total Sales:"
ws.Cells(lastRow + 3, 2).Value = totalSales
ws.Cells(lastRow + 4, 1).Value = "Top Region:"
ws.Cells(lastRow + 4, 2).Value = topRegion
ws.Cells(lastRow + 4, 3).Value = "Sales in Top Region:"
ws.Cells(lastRow + 4, 4).Value = maxSales
' Format the summary
ws.Range("A" & (lastRow + 2) & ":D" & (lastRow + 4)).Font.Bold = True
MsgBox "Data cleaned, highlighted, and summarized!", vbInformation
End Sub
Step 3: Run the Macro
Save the code and return to Excel (Alt + F11).
Press Alt + F8, select CleanHighlightSummarize, and click Run.
Alternatively, assign the macro to a button for easy access: Go to Developer > Insert > Button, draw a button on the sheet, and assign the macro.
What the Macro Does: Example in Action
Let’s say your initial dataset looks like this (simplified for clarity):
Product Name | Sales Amount | Region | Date |
Widget A | 12000 | East | 2025-05-01 |
Widget B | 8000 | West | 2025-05-02 |
(blank row) | |||
Widget A | 12000 | East | 2025-05-01 |
Widget C | 15000 | East | 2025-05-03 |
Widget D | 5000 | West | 2025-05-04 |
After Running the Macro:
Blank Rows Removed: The empty row is deleted.
Duplicates Removed: The duplicate entry for Widget A is removed, leaving one instance.
Highlighting: Rows for Widget A ($12,000) and Widget C ($15,000) are highlighted in green since their sales exceed $10,000.
Summary Added: At the bottom, a summary shows:
Total Sales: $36,000 (12,000 + 8,000 + 15,000 + 5,000)
Top Region: East (with $27,000 in sales, compared to West’s $13,000)
The updated sheet looks like this:
Product Name | Sales Amount | Region | Date |
Widget A | 12000 | East | 2025-05-01 |
Widget B | 8000 | West | 2025-05-02 |
Widget C | 15000 | East | 2025-05-03 |
Widget D | 5000 | West | 2025-05-04 |
Summary | |||
Total Sales: | 36000 | ||
Top Region: | East | Sales in Top Region: | 27000 |
Why This Trick is Interesting
Automation: It combines multiple tasks such as cleaning, formatting, and summarizing into one click, saving hours of manual work.
Dynamic Insights: The macro automatically identifies and highlights key data points (like high sales) and calculates metrics (like top region) without manual intervention.
Scalability: It works for datasets of any size, as it dynamically finds the last row and processes accordingly.
Customization: You can tweak the code to fit your needs to change the sales threshold, add more summary metrics, or modify the formatting style.
Pro Tip
To make this macro even more powerful, you could add error handling (e.g., checking if the sheet exists) or prompt the user to input the sales threshold instead of hardcoding $10,000. For example, add this at the start of the macro to prompt for the threshold:
vba
Dim salesThreshold As Double
salesThreshold = InputBox("Enter the sales threshold for highlighting:", "Sales Threshold", 10000)
Then replace If ws.Cells(i, 2).Value > 10000 with If ws.Cells(i, 2).Value > salesThreshold.
This macro trick showcases the power of Excel VBA to turn tedious data tasks into an efficient, insightful process which is perfect for anyone looking to impress their team with quick, professional results!