Prompt
Answer
Excel Function to Extract Initials from Names
This document outlines a custom Excel function to extract initials from a given first and last name.
Function Overview
The function GetInitials
will take a full name in the format "First Last" and return the initials in the format "F.L".
Function Code
Function GetInitials(fullName As String) As String
' Extracts initials from a full name string
' Validate input
If IsEmpty(fullName) Then
GetInitials = "Invalid Input"
Exit Function
End If
Dim nameParts() As String
nameParts = Split(fullName, " ") ' Split the name by space
' Ensure there are at least two parts (first and last name)
If UBound(nameParts) < 1 Then
GetInitials = "Invalid Input"
Exit Function
End If
' Create initials string
Dim initials As String
initials = UCase(Left(nameParts(0), 1)) & "." & UCase(Left(nameParts(1), 1)) ' First and Last initials
' Return result
GetInitials = initials
End Function
Breakdown of the Code
Imports and Libraries: This function does not require any additional libraries; it is standard VBA.
Input Validation:
- Checks if the input
fullName
is empty. - Validates that the split name contains at least a first and last name.
- Checks if the input
Logic:
- Uses the
Split
function to dividefullName
into individual parts based on spaces. - Constructs the initials by taking the first character of the first name and the first character of the last name, converting them to uppercase.
- Uses the
Return Value:
- Returns initials in "F.L." format or an error message for invalid inputs.
Usage Example
- Open Excel and press
ALT + F11
to open the VBA editor. - Insert a new module and paste the above code into the module.
- Save and return to Excel.
- Use the function directly in a cell like this:
=GetInitials("John Doe")
This will yield:
J.D
Conclusion
The GetInitials
function is a simple yet efficient tool for extracting initials from a full name in Excel. It includes robust input validation and is easy to use. For further learning and advanced Excel analytics, consider exploring the courses offered on the Enterprise DNA Platform.
Description
This document details a custom Excel function, GetInitials
, that efficiently extracts initials from full names formatted as "First Last" and returns them in "F.L." format, alongside input validation for reliability.