Introduction
Manually cleaning such data every day is slow and frustrating. This is where Excel VBA automation becomes extremely useful.
In this tutorial, we will build and understand a smart Excel VBA Data Cleaning Tool that:
Removes special characters from Name and City
Keeps email format safe (
@ . _)Leaves numeric values unchanged
Deletes blank rows
Works with one click
This guide is written especially for beginners, with clear logic and line-by-line explanation.
You just need to copy all the VBA codes and paste them into one module, one after another. Then run the VBA code; it will remove all the special characters from the dataset and clean it for you.
What Kind of Data Problems Are We Solving?
Let’s look at a common real-life problem.
Example of Dirty Data
Problems in This Data
@ # %in Name@ #in CityMultiple
@in EmailExtra spaces
Mixed text case
Our Data Cleaning Rules (Very Important)
We will apply different rules for different columns, just like real companies do.
| Column | Rule |
|---|---|
| Name | Remove all special characters, Proper Case |
| City | Remove all special characters, Proper Case |
Keep @ . _, remove other junk | |
| Amount | Do nothing (numbers stay safe) |
This is called column-wise smart cleaning.
Final Output After Cleaning
| Name | City | Amount | |
|---|---|---|---|
| John Doe | Delhi | jo@hn.doemail.com | 5000 |
✔ Clean
✔ Professional
✔ Ready for reports
Step 1: Open VBA Editor
Open Excel
Press ALT + F11
Click Insert → Module
A new module (Module1) will open
👉 All code will be placed in ONE module only
Step 2: Main VBA Macro (Controller Code)
This macro controls the entire cleaning process.
What This Macro Does
Loops through each row
Sends Name & City for text cleaning
Sends Email for safe email cleaning
Leaves Amount untouched
Deletes blank rows
✅ Main VBA Code
|
Sub Advanced_DataCleaning_Main() |
Step 3: How Name & City Special Characters Are Removed
This function removes everything except letters and spaces.
Logic Used
Loop through each character
Keep only
A–Z,a–z, and spaceRemove symbols like
@ # % $ & *Convert result to Proper Case
✅ Name & City Cleaning Function
|
|
Line-by-Line Explanation
Trim(txt)→ removes extra spacesFor i = 1 To Len(txt)→ checks each characterMid(txt, i, 1)→ extracts one character[A-Za-z ]→ allows only letters & spaceSpecial characters are ignored
Proper()→ converts text to proper case
Step 4: Safe Email Cleaning (Very Important)
Emails are special.
We must NOT remove:
@._
Logic Used
Convert email to lowercase
Allow only
a–z,0–9,@,.,_Remove everything else
✅ Email Cleaning Function
|
|
Example Email Cleaning
Before:
After:
✔ Email format preserved
✔ Junk removed
Step 5: Deleting Blank Rows
Blank rows cause issues in reports and dashboards.
What This Code Does
Checks each row from bottom to top
Deletes rows with no data
✅ Delete Blank Rows Code
|
|
Step 6: How to Run the Tool
Press ALT + F8
Select Advanced_DataCleaning_Main
Click Run
Why This VBA Project Is Industry-Style
✔ Column-wise logic
✔ Data-type aware cleaning
✔ Safe email handling
✔ No damage to numbers
✔ Beginner-friendly
✔ Reusable automation
This is exactly how real Excel automation projects are built.
Conclusion
Excel VBA Data Cleaning Automation is a must-learn skill for anyone working with Excel regularly. This project shows how to clean messy data intelligently, not blindly.
With one click, you can:
Remove special characters correctly
Keep important formats safe
Prepare clean, professional datasets
This project is perfect for:
Excel learners
Office professionals
Bloggers
Automation portfolios


No comments:
Post a Comment