Based on this answer, here's an elegant solution which works with any number of middle names:
=LEFT(A1,2)&LEFT(TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1))),2)
Explanation:
SUBSTITUTE(A1, " ", REPT(" ",LEN(A1)))
replaces the inter-word space(s) with spaces equal in number to the length of the entire string. Using the string length rather than an arbitrarily large number guarantees the formula works for any length string and means it does so efficiently.
RIGHT(space_expanded_string, LEN(A1))
extracts the rightmost word prepended by a bunch of spaces.*
TRIM(space_prepended_rightmost_word)
extracts the rightmost word.
LEFT(rightmost_word, 2)
extracts the first two characters of the rightmost word (last name).
*Caveat: If it's possible for a username to contain trailing spaces, you need to replace the first argument of SUBSTITUTE()
, i.e. A1
, with TRIM(A1)
. Leading spaces and multiple consecutive spaces between words are handled correctly just with A1
.
Fixing Your Attempt
Taking a closer look at your attempted solution, it looks like you were very close to a working formula to concatenate the first two letters of the first word (i.e. the first name) and the first two letters of the second word if it existed.
Note that if a username were to contain middle names, the corrected formula would incorrectly grab the first two letters from the first middle name instead of from the last name (assuming your intent is indeed to extract them from the last name).
Also, if all the usernames consist only of either a first name, or a first name and a last name, then the formula is needlessly complicated and can be simplified.
To see how the formula works and so fix it, it is easier if it is prettified, like so:
=
LEFT(A1,2) &
MID(
A1,
IFERROR(FIND(" ",A1), LEN(A1)) + 1,
IFERROR(
FIND(" ", SUBSTITUTE(A1," ","",1)),
LEN(A1)
)
- IFERROR(FIND(" ",A1), LEN(A1))
)
To understand how it works, first look at what happens when A1
contains no spaces (i.e. it contains a single name only). All the IFERROR()
functions evaluate to their second arguments since FIND()
returns a #VALUE!
error if the search string is not found in the target string:
=
LEFT(A1,2) &
MID(
A1,
LEN(A1) + 1,
LEN(A1)
-LEN(A1)
)
The third argument of MID()
evaluates to zero, so the function outputs ""
and the formula result is the first two characters of the single name.
Now look at when there are exactly two names (i.e there is exactly one space). The first and third IFERROR()
functions evaluate to their first arguments but the second one evaluates to its second argument since FIND(" ", SUBSTITUTE(A1," ","",1))
is attempting to find another space after removing the first, and only, one:
=
LEFT(A1,2) &
MID(
A1,
FIND(" ",A1) + 1,
LEN(A1)
- FIND(" ",A1)
)
Clearly, MID()
returns the second word (i.e. the last name) in its entirety, and the formula result is the first two characters of the first name followed by all the characters of the last name.
For completeness sake, we'll also look at the case where there are at least three names, although it should be fairly obvious now how to fix the formula. This time, all the IFERROR()
functions evaluate to their first arguments:
=
LEFT(A1,2) &
MID(
A1,
FIND(" ",A1) + 1,
FIND(" ", SUBSTITUTE(A1," ","",1))
- FIND(" ",A1)
)
It is slightly less clear than it was in the previous case, but MID()
returns exactly the entire second word (i.e. first middle name). Thus, the formula result is the first two characters of the first name followed by all the characters of the first middle name.
Obviously, the fix is to use LEFT()
to get the first two characters of the MID()
output:
=
LEFT(A1,2) &
LEFT(
MID(
A1,
IFERROR(FIND(" ",A1), LEN(A1)) + 1,
IFERROR(
FIND(" ", SUBSTITUTE(A1," ","",1)),
LEN(A1)
)
- IFERROR(FIND(" ",A1), LEN(A1))
),
2
)
The simplification I mentioned above is to replace LEFT(MID(…,…,…), 2)
with MID(…,…,2)
:
=
LEFT(A1,2) &
MID(
A1,
IFERROR(FIND(" ",A1), LEN(A1)) + 1,
2
)
or on one line:
=LEFT(A1,2)&MID(A1,IFERROR(FIND(" ",A1),LEN(A1))+1,2)
This is essentially PeterH's solution modified to also work with single names (in which case, the result is just the first two characters of the name).
Note: The prettified formulas actually work if entered.