How to Generate Unique Email Addresses in CSV Using VBA
Question details
The user needs to automatically generate unique, lowercase email addresses in a CSV file using VBA by concatenating names and location IDs.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating the creation of bulk email addresses from a source CSV file that contains users' names and unique location identifiers.
- Observed behavior
- The goal is to produce a new CSV file containing properly formatted, duplicate-free email addresses derived from the source data.
Ensure your source CSV file is properly formatted with names and location IDs in consistent columns, and verify that Developer tools and macros are enabled in your spreadsheet application.
Use a VBA Macro to Generate and Save Email Addresses
Run a custom VBA script to extract data from your CSV, construct unique email addresses, and save an updated CSV file automatically.
This VBA script opens a target CSV file, loops through the data rows, and constructs an email address by combining the first letter of the first name, the last name, and the location ID. It converts the string to lowercase using the LCase function and saves the output as a new CSV file to preserve your original data.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in your spreadsheet program.
Click 'Insert' from the top menu of the VBA editor and select 'Module' to create a blank script window.
Paste your email generation script into the module window. Ensure the script includes the Application.GetOpenFilename method to allow file selection, and a loop that constructs the email using the LCase() function.
Close the VBA editor and press Alt + F8. Select your macro name (e.g., CreateCSV) from the list and click 'Run'. Choose your source CSV file when the file explorer prompt appears.
The macro will process the data, generate the lowercase email addresses in the designated column, and automatically save a new file with '_new.csv' appended to the original filename.

Automate Email Generation with WPS Spreadsheet
WPS Spreadsheet fully supports advanced macros and VBA scripts, making it incredibly easy to process large CSV files and automatically generate bulk email addresses in seconds.
- 1. Open WPS Spreadsheet: Launch WPS Spreadsheet and navigate to the Developer tab on the top ribbon.
- 2. Launch the VBA Editor: Click 'Macros' or press Alt + F11 to open the built-in VBA Editor.
- 3. Insert the Script: Insert a new Module and paste your CSV email generation VBA script.
- 4. Run the Automation: Run the script to prompt for your CSV file and automatically generate the unique email addresses.

Frequently Asked Questions
How does the VBA script handle middle names or compound surnames?
A basic VBA script typically uses the Split function to separate a string by spaces and grabs the second element as the last name. For middle names or compound surnames, you need to add custom parsing logic using functions like InStr or Replace to ensure the correct name parts are extracted.
Why did my macro save the new CSV file without the email addresses?
This usually happens if the columns referenced in the VBA code do not match your actual CSV layout. Verify that your names and location IDs are in the exact columns specified in your script (for example, Column C and Column B), or update the column references in the code accordingly.
How do I ensure the generated email addresses are entirely lowercase?
You can ensure all characters are lowercase by wrapping your constructed email string in the LCase() function in VBA before writing the value back to the destination cell.
Can I run this VBA script directly on an already open workbook instead of an external CSV?
Yes. If you want to run the script on an open workbook rather than prompting for an external file, you must modify the script to reference ActiveWorkbook and ActiveSheet instead of opening a file via Application.GetOpenFilename.




