If you don't mind, will you explain the formula?
Sure. This is a pretty slick formula once you "see" how it works.
=TRIM(RIGHT(SUBSTITUTE(TRIM(A1)," ",REPT(" ",255)),255))
A1 = Washington DC 20005
Let's break it down into steps:
The first thing that happens is the inner TRIM function gets executed. TRIM
removes leading/trailing spaces and extra spaces between words in the
string. If A1 contained:
<space>Washington<2 spaces>DC<2 spaces>20005<space>
TRIM(A1) removes all those extra spaces so you end up with:
Washington DC 20005
That inner TRIM function might not be needed but it doesn't hurt anything
having it in there just in case!
The result of the inner TRIM function is then passed to the SUBSTITUTE
function. The SUBSTITUTE function lets you substitute character(s) in a
string with other characters.
SUBSTITUTE("Washington DC 20005"," ",REPT(" ",255))
In the above, we're going to substitute each instance of the space character
with REPT(" ",255). The REPT function let's you repeat (REPT) a character(s)
n times. In this case REPT(" ",255) means repeat a space character 255
times. So, what the SUBSTITUTE function is going to do is substitute each
instance of the space character in the string with 255 space characters.
That might sound kind of strange but you'll see later on just how that comes
into play. Since I can't show all those spaces in this example (for
practical reasons!), this is what the result of the SUBSTITUTE function
looks like:
Washington_____DC_____20005
Just imagine that those underscores are 255 space characters.
Ok, the result of the SUBSTITUTE function is then passed to the RIGHT
function. The RIGHT function lets you extract a portion of the string
starting from the rightmost character and going to the left.
RIGHT("Washington_____DC_____20005",255)
That means we want to extract 255 characters starting from the rightmost
character in the string.
Remember, we used SUBSTITUTE to "pad" the number of spaces between words so
there's <255 spaces>20005 or a total of 260 characters. We told RIGHT we
wanted the last 255 characters so the result of the RIGHT function is:
<200 spaces>20005 = 255 characters
And we finally get to the last step!
The result of the RIGHT function is then passed to the outer TRIM function
which removes those 200 leading spaces and the final result of the formula
is:
20005
Pretty slick, ain't it? I don't know who the originator of that formula is
but my hats off to them!
Why do we use 255 as arguments to REPT and RIGHT?
255 is just an arbitrary number that's large enough to insure that we get
the result we're looking for. We're assuming that the last word in the
string won't be more than 255 characters which is a fairly safe bet.
exp101