简体   繁体   中英

How to use SUBSTITUTE() for a range of values?

Let's say that there are multiple values like such:

Address
123 Fake Street, Philadelphia, pa
456 Real Street, Houston, tx
789 Soho Street, New York, ny

Originally, I thought I could create a lookup table to search for "pa", "tx", and "ny" so that they would get upper'd: "PA", "TX", "NY".

Ideally, I want to write a function that would have all of the upper values in a new column. Originally, something like this:

=substitute(O33,Q31:Q35,R31:R35)

with Q31:Q35 being the range of lower states, and R31:R35 being the upper states. However, that didn't work out the way I wanted. Is there a way to do the replacement of lower case states without hard coding? I thought a lookup table would work, but it didn't.

I want the end result to be something like this:

Address                                     New Address
123 Fake Street, Philadelphia, pa           123 Fake Street, Philadelphia, PA
456 Real Street, Houston, tx                456 Real Street, Houston, TX
789 Soho Street, New York, ny               789 Soho Street, New York, NY

Without doing this for every row of New Address:

substitute(A2, "pa", "PA")
substitute(A3, "tx", "TX")
substitute(A4, "ny", "NY")

A lookup table should work, try using a vlookup. You want to take the 2 right characters from the address and use that as the lookup value to return the upper case version, roughly like this (assume address in A1):

new value = left(A1, len(A1)-2) & vlookup(right(A1, 2), Q31:R35, 2, false)

also if there is no complex mapping ie you are always just capitalizing the last 2 characters you could do this:

new value = left(A1, len(A1)-2) & upper(right(A1, 2))

If your only goal is to replace the last two characters with their uppercase counterparts then you could use this formula:

=LEFT(A1,LEN(A1)-2)&UPPER(RIGHT(A1,2))

It trims the text to exclude the right two characters while creating a second text string of the right two where it capitalizes them. Then it concatenates the two strings into one.

The trouble with using SUBSTITUTE is that it will replace all occurrences of the last two characters with their uppercase versions. So "123 Funny Street, New York, ny" would become "123 FunNY Street, New York, NY".

Try an array formula.

in A2 until A4 you have your addresses

123 Fake Street, Philadelphia, pa
456 Real Street, Houston, tx
789 Soho Street, New York, ny

in Q31:Q33 and R31:R33 you have :

pa  PA
tx  TX
ny  NY

In E2, enter this formula :

=SUBSTITUTE(A2:A4;Q31:Q33;R31:R33)

In the formula bar type CTRL SHIFT and ENTER. You will see {} in the formula like this :

{=SUBSTITUTE(A2:A4;$Q$31:$Q$33;$R$31:$R$33)}

Then copy the formula in E3 and E4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM