+ Reply to Thread
Results 1 to 20 of 20

Reducing a formula

  1. #1
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Reducing a formula

    Could this formula be cut down with the Choose function? The state abbreviations appear nowhere on the spreadsheet. To the left of the formula is either an 8 digit number or a 9 digit number. If it is 8 I am only concerned with the first number. If it is 9 digits I want the first two numbers.

    =IF(LEN(D2)=8,(IF((LEFT(D2,"01")="1"),"AL",(IF((LEFT(D2,"01")="2"),"AK",(IF((LEFT(D2,"01")="3"),"AZ",(IF((LEFT(D2,"01")="4"),"AR",(IF((LEFT(D2,"01")="5"),"CA",IF((LEFT(D2,"01")="6"),"CO",(IF((LEFT(D2,"01")="7"),"CT",(IF((LEFT(D2,"01")="8"),"DE",(IF((LEFT(D2,"01")="9"),"DC"))))))))))))))))),(IF((LEFT(D2,"02")="10"),"FL",IF((LEFT(D2,"02")="11"),"GA",IF((LEFT(D2,"02")="12"),"HI",IF((LEFT(D2,"02")="13"),"ID",IF((LEFT(D2,"02")="14"),"IL",IF((LEFT(D2,"02")="15"),"IN",IF((LEFT(D2,"02")="16"),"IA",IF((LEFT(D2,"02")="17"),"KS",IF((LEFT(D2,"02")="18"),"KY",IF((LEFT(D2,"02")="19"),"LA",IF((LEFT(D2,"02")="20"),"ME",IF((LEFT(D2,"02")="21"),"MD",IF((LEFT(D2,"02")="22"),"MA",IF((LEFT(D2,"02")="23"),"MI",IF((LEFT(D2,"02")="24"),"MN",IF((LEFT(D2,"02")="27"),"MT",IF((LEFT(D2,"02")="25"),"MS",IF((LEFT(D2,"02")="26"),"MO",IF((LEFT(D2,"02")="28"),"NE",IF((LEFT(D2,"02")="29"),"NV",IF((LEFT(D2,"02")="30"),"NH",IF((LEFT(D2,"02")="31"),"NJ",IF((LEFT(D2,"02")="32"),"NM",IF((LEFT(D2,"02")="33"),"NY",IF((LEFT(D2,"02")="34"),"NC",IF((LEFT(D2,"02")="35"),"ND",IF((LEFT(D2,"02")="36"),"OH",IF((LEFT(D2,"02")="37"),"OK",IF((LEFT(D2,"02")="38"),"OR",IF((LEFT(D2,"02")="39"),"PA",IF((LEFT(D2,"02")="40"),"RI",IF((LEFT(D2,"02")="41"),"SC",IF((LEFT(D2,"02")="42"),"SD",IF((LEFT(D2,"02")="43"),"TN",IF((LEFT(D2,"02")="44"),"TX",IF((LEFT(D2,"02")="45"),"UT",IF((LEFT(D2,"02")="46"),"VT",IF((LEFT(D2,"02")="47"),"VA",IF((LEFT(D2,"02")="48"),"WA",IF((LEFT(D2,"02")="49"),"WV",IF((LEFT(D2,"02")="50"),"WI",IF((LEFT(D2,"02")="51"),"WY",IF((LEFT(D2,"02")="60"),"AS",IF((LEFT(D2,"02")="61"),"FM",IF((LEFT(D2,"02")="66"),"GU",IF((LEFT(D2,"02")="67"),"MH",IF((LEFT(D2,"02")="68"),"MP",IF((LEFT(D2,"02")="69"),"PW",IF((LEFT(D2,"02")="70"),"CN",IF((LEFT(D2,"02")="72"),"PR",IF((LEFT(D2,"02")="75"),"TT",IF((LEFT(D2,"02")="78"),"VI",))))))))))))))))))))))))))))))))))))))))))))))))))))))



    Moderator's Note: This post was moved from this thread as per the Forum Rules. Please take a moment to read the rules, you are expected to abide them. Thanks.
    Last edited by JBeaucaire; 11-29-2012 at 06:14 PM. Reason: Moved to thread of its own.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Reducing a formula

    Can't you create a lookup table with your numbers in the first column and the state abbreviations in the seconds, then use VLOOKUP to just lookup the value in the table?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Reducing a formula

    It would not be practical to do that.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Reducing a formula

    Lookup tables are extremely practical and easier to read than formulas like the one created above. With the work done once of creating the reference table, your formulas would then be remarkably short.

    Can you provide several examples of the text strings that would be in D2?

  5. #5
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Reducing a formula

    The true portion of the IF statement would reference a number such as 11494359, the false portion would reference a nine character number 245930925. All the numbers will be different. I do not want to use a table, because this is a once a month exercise with a different spreadsheet, multiple users, etc. The formula works fine, I just wanted to know if it was possible to write it with a different function that would reduce the size.

  6. #6
    Forum Expert Ace_XL's Avatar
    Join Date
    06-04-2012
    Location
    UAE
    MS-Off Ver
    2016
    Posts
    6,074

    Re: Reducing a formula

    If you insist to have thsi in one formula try something on these lines..

    =IF(LEN(D2)=8,choose(LEFT(D2,1)*1,"AL","AK","AZ","AR",......................),choose(left(d2,2)*1,"FL","GH","AI",...................))
    Life's a spreadsheet, Excel!
    Say thanks, Click *

  7. #7
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Reducing a formula

    What would be another option? Using the choose function would be just as long. The only other way I see is to build an access table, and create a macro.

  8. #8
    Forum Expert Ace_XL's Avatar
    Join Date
    06-04-2012
    Location
    UAE
    MS-Off Ver
    2016
    Posts
    6,074

    Re: Reducing a formula

    The CHOOSE function is not even close to being as long.

    All you need to do is fill in the expected values in the dotted spaces in my above formula with the 2 letter abbr.

    Please Login or Register  to view this content.
    P.S. Having a refernce table works much better

  9. #9
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Reducing a formula

    1=AL, 2=AK, 3=AZ, 4=AR, etc through every state and u.s. territory. I do not want to use a table, because our system gets bogged down in high traffic periods (when i would want to be using the formula), and referencing a table could lock my system up. I do not see how the choose function would shorten the formula when I have 62 combinations.

  10. #10
    Forum Expert Ace_XL's Avatar
    Join Date
    06-04-2012
    Location
    UAE
    MS-Off Ver
    2016
    Posts
    6,074

    Re: Reducing a formula

    Did you try my formula in Post # 8? Does it give desired results?

    As a comparison..

    Your original gargantuan formula - 1855 characters
    Suggested formula - 386 characters

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Reducing a formula

    Quote Originally Posted by Jaypelt View Post
    ....our system gets bogged down in high traffic periods (when i would want to be using the formula), and referencing a table could lock my system up...
    People get the oddest ideas. There is no possible way a reference table would be the cause of your system locking, in fact the exact opposite is true. Your existing formula can do upwards of 60+ calculcations in a single cell. Creating a lookup table means the needed value is found every time in a single calculation... sixty fewer calculations per cell.

    You've experienced something in the past while using Excel that has left you with false impressions of what really happened. The lookup table IS the correct solution for this 'exercise'.

    Ace's Choose suggestion is an excellent next step. I fear you are tossing out the correct solutions given to you for no reason, and not even trying them. That is unfortunate.

  12. #12
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Reducing a formula

    I did not mean to infer that having a table would lock my system up; in high usage our server gets really slow (being improved); it would be exhausting trying to reference a table when opening a file takes 10 minutes.

  13. #13
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Reducing a formula

    I misread the formula at first. I think this is exactly what I need.

  14. #14
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Reducing a formula

    =IF(LEN(D2)=8,CHOOSE(LEFT(D2,1)*1,"AL","AK","AZ","AR","CA","CO","CT","DE","DC"),CHOOSE(LEFT(D2,2)*1,,,,,,,,,"FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY",,,,,,,,,"AS","FM",,,,,"GU","MH","MP","PW","CN",,"PR",,,"TT",,,"VI"))


    Worked beautifully, thank-you. I had not used the choose function before, was unsure about how to go about writing it.

  15. #15
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,700

    Re: Reducing a formula

    You don't even need to have the IF, you can use a single CHOOSE function like this

    =CHOOSE(LEFT(D2,LEN(D2)-7)+0,"AL".....etc. etc.)

    although I also think that a 2 column table, as JB suggested, would be the optimal approach (table can be on the same worksheet or on another worksheet in the same workbook so you wouldn't be opening anything that's not already open), name your table States and then this is the whole formula

    =VLOOKUP(LEFT(D2,LEN(D2)-7)+0,States,2,0)
    Last edited by daddylonglegs; 11-30-2012 at 12:54 PM.
    Audere est facere

  16. #16
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Reducing a formula

    Glad you got what you needed from Ace.

    BTW, I would never recommend putting the reference table in a separate workbook, rather it, too, would be in the same workbook where the formulas are being used.


    If that takes care of your original query, please select Thread Tools from the menu above and mark the thread as solved. Thanks.

  17. #17
    Registered User
    Join Date
    11-29-2012
    Location
    ystfast
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Reducing a formula

    I am well versed in Vlookups and using tables. If I was accomplished enough to write in VBA I would have gone that route. I wanted a stand alone formula in this instance that referenced nothing else, quick, easy, and precise. Thank-you for all the replies.

  18. #18
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Reducing a formula

    All the commas in the formula is ...
    quick, easy, and precise
    ...


    A Lookup table is without a doubt the way to go.

    An alternative to DLLs'
    CHOOSE(LEFT(D2,LEN(D2)-7)+0
    might be ...
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Marcol; 11-30-2012 at 02:04 PM. Reason: Added Attachment
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  19. #19
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,700

    Re: Reducing a formula

    Quote Originally Posted by Marcol View Post
    INT(D2/10^7)
    I like that.....

    ......I'd be wary of using LOOKUP, though, Marcol. Perhaps it isn't possible but if D2 starts with 65 (out of 9 digits) then you'll get a "false match" with 61 as the next lowest number on the list. VLOOKUP (with FALSE or zero as 4th argument) will give you #N/A and the CHOOSE approach would give you zero, both of which should alert you to the problem....

  20. #20
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Reducing a formula

    Aye, I would normally use INDEX(),MATCH() on a lookup that size
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1