RIGHT & UPPER Function help

R

roger lewis

I am working in Excel 2002. The postal address is in cell A1 with the
postcode. I am looking to take the postcode out of the cell and put in new
cell and turn into CAPITAL letters.
1 The High Street London ln22 4tb
I am struggling with the function for this. I am at =RIGHT(A1,8) which
takes the postcode from the end of the address and enters ino cell A2 but I
thought i may have been able to add the UPPER to the end of the function but
this does not work. I can do this as =UPPER(A2) in cell A3 but was hoping to
undertake the augument in the same cell. Also can I adapt the augument to
include where I only have 6 characters ie LN24tb?

Many thanks

Roger
 
G

Gary''s Student

Hi Roger:

The first question is quite easy, just nest the functions:

=UPPER(RIGHT(A1,8))

The second question is not as easy.
 
R

roger lewis

Thanks Gary I was nearly there. Works ok now on first part. Is the second
part undoable?

Regards

Roger
 
G

Gary''s Student

The difficulty is determining how much of the string to grab. Look at the
last six characters. If they contain a blank, we want eight. If they do not
contain a blank, then we want six:

=UPPER(IF(LEN(SUBSTITUTE(RIGHT(A1,6)," ",""))=6,RIGHT(A1,6),RIGHT(A1,8)))

As you see, this formula is more complex, but usable.
 
D

Dave Peterson

Maybe...

=UPPER(MID(RIGHT(A1,8),1,3))&RIGHT(A1,3)

But that depends on the final 8 characters always being the same format:
xxxx yyy
 
R

Rick Rothstein \(MVP - VB\)

Although I am not from Great Britain, I am pretty sure the postal codes over
there can be of either of these formats... "ccc ccc" or "cccc ccc" where 'c'
stands for character (either digit or letter). If you will ultimately need
to cater to both of those formats (plus your case of no internal space),
then I think this formula will do that...

=IF(LEFT(RIGHT(A1,8))<>" ",IF(LEFT(RIGHT(A1,7))<>"
",RIGHT(A1,8),RIGHT(A1,6)),TRIM(RIGHT(A1,7)))

While you didn't ask for this functionality, if you would like to insert any
missing space characters, while still catering to the two possible formats
listed above (plus your case of no internal space), then I think this
formula will do that...

=TRIM(IF(LEFT(RIGHT(A28,8))<>" ",IF(LEFT(RIGHT(A28,7))<>"
",RIGHT(A28,8),SUBSTITUTE(RIGHT(A28,6),RIGHT(A28,3),"
"&RIGHT(A28,3))),SUBSTITUTE(RIGHT(A28,7),RIGHT(A28,3)," "&RIGHT(A28,3))))

Rick
 
R

Rick Rothstein \(MVP - VB\)

I just noticed that on my newsreader the two formulas I posted were broke
apart at the space characters that should be between quote marks. I also
notice that I didn't adjust the cell reference for my second formula back to
A1 from the A28 cell I used for my testing. Here are the two formulas again,
but this time I have broken them apart so that the break points are not at
the space characters (and the second one adjusted for its cell reference)...

=IF(LEFT(RIGHT(A1,8))<>" ",IF(LEFT(RIGHT(A1,7))
<>" ",RIGHT(A1,8),RIGHT(A1,6)),TRIM(RIGHT(A1,7)))

=TRIM(IF(LEFT(RIGHT(A1,8))<>" ",IF(LEFT(RIGHT
(A1,7))<>" ",RIGHT(A1,8),SUBSTITUTE(RIGHT(A1,6),
RIGHT(A1,3)," "&RIGHT(A1,3))),SUBSTITUTE(RIGHT
(A1,7),RIGHT(A1,3)," "&RIGHT(A1,3))))

Rick
 
R

Ron Rosenfeld

I am working in Excel 2002. The postal address is in cell A1 with the
postcode. I am looking to take the postcode out of the cell and put in new
cell and turn into CAPITAL letters.
1 The High Street London ln22 4tb
I am struggling with the function for this. I am at =RIGHT(A1,8) which
takes the postcode from the end of the address and enters ino cell A2 but I
thought i may have been able to add the UPPER to the end of the function but
this does not work. I can do this as =UPPER(A2) in cell A3 but was hoping to
undertake the augument in the same cell. Also can I adapt the augument to
include where I only have 6 characters ie LN24tb?

Many thanks

Roger

I thought it was a requirement that there be a <space> prior to the last 3
characters.

According to http://www.zipcodeworld.com/addressing/greatbritain.htm valid
postcodes are in one of the following formats:

The postcode will be in one of the six following formats. 'A' represents a
letter of the alphabet and 'N' a digit:

Format Example
AN NAA M2 5BQ
ANN NAA M34 4AB
AAN NAA CR0 2YR
AANN NAA DN16 9AA
ANA NAA W1A 4ZZ
AANA NAA EC1A 1HQ

Also, there are a finite number of postcode areas.

Depending on all of your requirements, it would be possible to at least ensure
that the postcode is valid; it would also be possible to insert a <space> if
one does not exist.

I would not want to develop a method to validate that the address and postcode
go together, however. On that note I would point out that, according to the
above mentioned site, LN refers to Lincoln, UK and not London, UK.

So, do you want any type of validation?
Will some of your postcodes NOT have the required <space>?
Will some of your address lines NOT have postcodes at the end?

In any event, the User Defined Function below will extract anything that looks
similar to a valid postcode, with or without the required space, provided it is
at the end of a line of text.

It will return the result capitalized, and also with the last three characters
preceded by a <space>.

To enter this, <alt-F11> opens the VB Editor. Ensure your project is
highlighted in the project explorer window, then Insert/Module and paste the
code below into the window that opens.

If GB develops new postal areas, some modification of the code below will be
required, but that should be simple to do.

To use this, enter the formula =PostCode(cell_ref) where cell_ref refers to a
single cell.

This code should work even if the entire address is in a single cell.

====================================================
Option Explicit
Function PostCode(Str As String) As String
Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
With re
.Global = True
.MultiLine = True
.ignorecase = True
'Valid postcode format with optional <space>
.Pattern = "(\b\w{1,2}\d\w?)\s?(\d[ABD-HJLNP-UW-Z]{2}\b)\s*$"
End With

If re.test(Str) = True Then
Set mc = re.Execute(Str)
PostCode = UCase(mc(0).submatches(0) _
& " " & mc(0).submatches(1))
End If
End Function
===================================
--ron
 
R

Ron Rosenfeld

=TRIM(IF(LEFT(RIGHT(A28,8))<>" ",IF(LEFT(RIGHT(A28,7))<>"
",RIGHT(A28,8),SUBSTITUTE(RIGHT(A28,6),RIGHT(A28,3),"
"&RIGHT(A28,3))),SUBSTITUTE(RIGHT(A28,7),RIGHT(A28,3)," "&RIGHT(A28,3))))

Using this formula on:

1 the high street london A33AB

I get as a result:

on A33AB


--ron
 
R

Rick Rothstein \(MVP - VB\)

I guess we could build in some error checking for mistyping, but a
5-character string of text cannot be a postal code... I simply assumed all
entries had legitimate postal codes at the end.

Rick
 
R

Ron Rosenfeld

I guess we could build in some error checking for mistyping, but a
5-character string of text cannot be a postal code... I simply assumed all
entries had legitimate postal codes at the end.

Well, in his example he presented a postal code that did not have an included
space. If we are going to allow for the absence of the space, then a five
character postal code, for GB, is possible, and it would be in the format of
ANNAA (where A is a letter of the alphabet, and N a number).

Here is a valid GB address:

10B Barry Jackson Tower
Estone Walk
BIRMINGHAM
B6 5BA
UNITED KINGDOM

Note that if the postal code were missing the <space>, it would be only five
characters.
--ron
 
R

Rick Rothstein \(MVP - VB\)

I guess we could build in some error checking for mistyping, but a
Well, in his example he presented a postal code that did not have an
included
space. If we are going to allow for the absence of the space, then a five
character postal code, for GB, is possible, and it would be in the format
of
ANNAA (where A is a letter of the alphabet, and N a number).

Here is a valid GB address:

10B Barry Jackson Tower
Estone Walk
BIRMINGHAM
B6 5BA
UNITED KINGDOM

Note that if the postal code were missing the <space>, it would be only
five
characters.

Hmm! You are right... a "cc ccc" postal code is allowed (I missed that in my
check). Okay, I think this formula handles that situation...

=TRIM(IF(LEFT(RIGHT(A1,8))<>" ",IF(LEFT(RIGHT(A1,7))<>"
",IF(LEFT(RIGHT(A1,6))=" ",SUBSTITUTE(RIGHT(A1,5),RIGHT(A1,3),"
"&RIGHT(A1,3)),RIGHT(A1,8)),SUBSTITUTE(RIGHT(A1,6),RIGHT(A1,3),"
"&RIGHT(A1,3))),SUBSTITUTE(RIGHT(A1,7),RIGHT(A1,3)," "&RIGHT(A1,3))))

Note: I have not gone back to modify the first formula because I'm thinking
just preserving the same incorrect postal-code entry that the data contains
would not make sense; hence, I am suggesting the OP use the above formula
for his application (and not use the first one I posted at all since it
incorrectly handles the 5-character postal code you pointed out to me).

Rick
 
R

Ron Rosenfeld

=TRIM(IF(LEFT(RIGHT(A1,8))<>" ",IF(LEFT(RIGHT(A1,7))<>"
",IF(LEFT(RIGHT(A1,6))=" ",SUBSTITUTE(RIGHT(A1,5),RIGHT(A1,3),"
"&RIGHT(A1,3)),RIGHT(A1,8)),SUBSTITUTE(RIGHT(A1,6),RIGHT(A1,3),"
"&RIGHT(A1,3))),SUBSTITUTE(RIGHT(A1,7),RIGHT(A1,3)," "&RIGHT(A1,3))))

That does seem to work better.

It does still assume, however, that the ending of the line will always be a
valid post code (and also that there will not be any trailing spaces after the
post code). But since the OP has not posted back with any more requirements,
it's probably not worthwhile to go further.

(Besides, I like my regex solution better <g>)

By the way, I still had to edit your formula in order to get it to work
properly, after pasting it in. It inserted LF character in addition to a
<space>.

I suspect that is because, even though you may be "breaking" the lines in the
formula bar, you are entering the "breaks" within the quoted space. So when I
paste it into my formula bar, <space><LF> gets inserted.

I would suggest breaking the lines outside of quoted text.

Yes, I know I suggested doing it that way, but I guess I've never entered the
breaks within quoted text :-(

--ron
 
R

roger lewis

Many thanks to you all for taking the time on a Sunday to resolving this
conundrum. I will work my way through the replies and try and understand
them.

regards

Roger
Ron Rosenfeld said:
I am working in Excel 2002. The postal address is in cell A1 with the
postcode. I am looking to take the postcode out of the cell and put in
new
cell and turn into CAPITAL letters.
1 The High Street London ln22 4tb
I am struggling with the function for this. I am at =RIGHT(A1,8) which
takes the postcode from the end of the address and enters ino cell A2 but
I
thought i may have been able to add the UPPER to the end of the function
but
this does not work. I can do this as =UPPER(A2) in cell A3 but was hoping
to
undertake the augument in the same cell. Also can I adapt the augument to
include where I only have 6 characters ie LN24tb?

Many thanks

Roger

I thought it was a requirement that there be a <space> prior to the last 3
characters.

According to http://www.zipcodeworld.com/addressing/greatbritain.htm
valid
postcodes are in one of the following formats:

The postcode will be in one of the six following formats. 'A' represents a
letter of the alphabet and 'N' a digit:

Format Example
AN NAA M2 5BQ
ANN NAA M34 4AB
AAN NAA CR0 2YR
AANN NAA DN16 9AA
ANA NAA W1A 4ZZ
AANA NAA EC1A 1HQ

Also, there are a finite number of postcode areas.

Depending on all of your requirements, it would be possible to at least
ensure
that the postcode is valid; it would also be possible to insert a <space>
if
one does not exist.

I would not want to develop a method to validate that the address and
postcode
go together, however. On that note I would point out that, according to
the
above mentioned site, LN refers to Lincoln, UK and not London, UK.

So, do you want any type of validation?
Will some of your postcodes NOT have the required <space>?
Will some of your address lines NOT have postcodes at the end?

In any event, the User Defined Function below will extract anything that
looks
similar to a valid postcode, with or without the required space, provided
it is
at the end of a line of text.

It will return the result capitalized, and also with the last three
characters
preceded by a <space>.

To enter this, <alt-F11> opens the VB Editor. Ensure your project is
highlighted in the project explorer window, then Insert/Module and paste
the
code below into the window that opens.

If GB develops new postal areas, some modification of the code below will
be
required, but that should be simple to do.

To use this, enter the formula =PostCode(cell_ref) where cell_ref refers
to a
single cell.

This code should work even if the entire address is in a single cell.

====================================================
Option Explicit
Function PostCode(Str As String) As String
Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
With re
.Global = True
.MultiLine = True
.ignorecase = True
'Valid postcode format with optional <space>
.Pattern = "(\b\w{1,2}\d\w?)\s?(\d[ABD-HJLNP-UW-Z]{2}\b)\s*$"
End With

If re.test(Str) = True Then
Set mc = re.Execute(Str)
PostCode = UCase(mc(0).submatches(0) _
& " " & mc(0).submatches(1))
End If
End Function
===================================
--ron
 
R

Rick Rothstein \(MVP - VB\)

(Besides, I like my regex solution better <g>)

LOL... but of course. said:
By the way, I still had to edit your formula in order to get it to work
properly, after pasting it in. It inserted LF character in addition to a
<space>.

I suspect that is because, even though you may be "breaking" the lines in
the
formula bar, you are entering the "breaks" within the quoted space. So
when I
paste it into my formula bar, <space><LF> gets inserted.

Isn't having to eat the LF standard for long formulas (or did I
misunderstand you)? As long as one leaves the spaces after the quote marks,
all should be well (providing it is realized the space is there, of
course<g>). Here is the formula broken apart so the breaks do not occur at a
space character...

=TRIM(IF(LEFT(RIGHT(A1,8))<>" ",IF(LEFT(RIGHT(A1,7))
<>" ",IF(LEFT(RIGHT(A1,6))=" ",SUBSTITUTE(RIGHT(A1,5),
RIGHT(A1,3)," "&RIGHT(A1,3)),RIGHT(A1,8)),SUBSTITUTE(
RIGHT(A1,6),RIGHT(A1,3)," "&RIGHT(A1,3))),SUBSTITUTE(
RIGHT(A1,7),RIGHT(A1,3)," "&RIGHT(A1,3))))

Rick
 
R

Ron Rosenfeld

Isn't having to eat the LF standard for long formulas (or did I
misunderstand you)? As long as one leaves the spaces after the quote marks,
all should be well (providing it is realized the space is there, of
course<g>). Here is the formula broken apart so the breaks do not occur at a
space character...

=TRIM(IF(LEFT(RIGHT(A1,8))<>" ",IF(LEFT(RIGHT(A1,7))
<>" ",IF(LEFT(RIGHT(A1,6))=" ",SUBSTITUTE(RIGHT(A1,5),
RIGHT(A1,3)," "&RIGHT(A1,3)),RIGHT(A1,8)),SUBSTITUTE(
RIGHT(A1,6),RIGHT(A1,3)," "&RIGHT(A1,3))),SUBSTITUTE(
RIGHT(A1,7),RIGHT(A1,3)," "&RIGHT(A1,3))))

Rick

That one works fine.

I don't think I was specific about not putting the LF inside quotes. If it's
not inside quotes, then everything works fine, though.
--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top