VBA & Macro Problems

How to Extract and Combine User Name Title and Surname in Excel VBA

Jason MillerJason Miller Nov 4, 2026 869 views

Question details

The user needs to modify an Excel VBA macro to correctly extract and combine a title and surname from Application.UserName instead of just outputting the title.

How to Extract and Combine User Name Title and Surname in Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Using a Worksheet_Change macro to automatically populate a user's full name (title and surname) into a specific column when another column is modified.
Observed behavior
The current VBA code successfully detects the title (e.g., Mr., Captain) but fails to append the surname, resulting in only the title being written to the target cell.
Before you start

Before modifying your VBA code, verify that the Application.UserName property contains the expected formatting for titles and surnames on your system, and back up your current macro script.

Solution 1Recommended

Update VBA Code to Concatenate Title and Surname

Modify your Worksheet_Change macro to normalize the username, extract the surname, and properly combine it with the detected title.

When working with Application.UserName, the macro must isolate the title using a case-insensitive string comparison and then parse the remainder of the string to capture the surname.

It is crucial to temporarily disable application events using Application.EnableEvents = False before writing the concatenated string to the worksheet. This prevents the Worksheet_Change event from calling itself and creating an infinite loop. Always remember to re-enable it afterward using an error-handler.

1
Open the VBA Editor

Press Alt + F11 in Excel to open the Visual Basic for Applications (VBA) Editor.

2
Locate Worksheet_Change Event

In the Project Explorer, double-click the specific Worksheet object containing the Worksheet_Change event macro you want to edit.

3
Detect Title and Extract Surname

Update your case-insensitive title detection logic to identify prefixes like 'Mr.' or 'Captain'. Use string manipulation functions like Split() or Mid() to isolate the surname from Application.UserName.

4
Concatenate Variables

Combine the extracted title and surname into a single variable (e.g., FullName = Title & " " & Surname).

5
Implement Event and Error Handling

Wrap your cell output code (e.g., Range("G1").Value = FullName) between Application.EnableEvents = False and Application.EnableEvents = True. Add an On Error GoTo ErrorHandler block to ensure events are restored if an error occurs.

Update VBA Code to Concatenate Title and Surname
EnableEvents Caution: If a macro terminates unexpectedly while Application.EnableEvents is False, events will remain disabled until manually reset in the Immediate Window or until Excel is restarted.
Advanced Spreadsheet Management

Use WPS Spreadsheet for Robust VBA and Macro Support

WPS Office offers robust support for VBA and Macros, allowing you to easily write, debug, and run custom scripts like Worksheet_Change events. You can manage complex data automation tasks effortlessly with an interface tailored for productivity.

  1. 1. Enable Developer Tools: Open WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled in the ribbon.
  2. 2. Open the VBA Editor: Click on the Developer tab and select Visual Basic, or press Alt + F11 to launch the integrated VBA editor.
  3. 3. Insert or Edit Macro Code: Double-click the desired Worksheet in the Project Explorer, paste your Worksheet_Change event code for the title and surname, and save your file as a macro-enabled workbook.
Full compatibility with Microsoft Excel VBA macros (.xlsm and .xlsb formats).Built-in Developer tools to easily edit, debug, and test Visual Basic code.Lightweight installation with high processing speed for complex automated tasks.Free to use core spreadsheet functionalities with a familiar user interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Worksheet_Change macro crash or freeze Excel when writing data?

If your macro writes data to a cell, it triggers another Worksheet_Change event, causing an infinite loop. To prevent this, use Application.EnableEvents = False before making changes to the worksheet, and set it back to True immediately afterward.

How can I extract just the last name from a full name in Excel VBA?

You can use the VBA Split() function to divide the full name string into an array based on space delimiters. The last element in that array (found using UBound()) typically represents the surname.

How do I make a string comparison case-insensitive in VBA?

You can make string comparisons case-insensitive by adding Option Compare Text at the top of your module, or by converting both strings to uppercase or lowercase using the UCase() or LCase() functions before comparing them.

Where does Application.UserName get its value from?

In Excel, Application.UserName retrieves the name registered in the Excel Options under the 'General' tab. It can be modified manually by the user within the application settings.