+ Reply to Thread
Results 1 to 6 of 6

Thread: Stuck on a large nested function...

  1. #1
    Registered User
    Join Date
    08-27-2011
    Location
    NH
    MS-Off Ver
    Excel 2003
    Posts
    2

    Question Stuck on a large nested function...

    Hello - I am stuck on creating a function that I think I am pretty close with but I can't quite get the logic right. I have a large dataset (sample attached) with varying formats of a code field. What the codes do have in common is that there is a color code after the last dash. But sometimes the code has 1 or 2 dashes. Overall the goal is:
    1. Isolate the characters after the last dash in the code field - this is the color code
    2. Using VLOOKUP assign a new color code from the Lookup Table.
    3. Replace the original color code from step1 with the returned new color code from the VLOOKUP in step2 - this will create a new code which is the same as the original with the exception of the color code (see attachment for example)
    4. Remove all dashes from the new code and concatenate a K in the front.

    I am a SAS programmer by trade and I can solve the problem in SAS via a SCAN function but this logic can't use SAS and needs to be maintained in Excel. Grrr...very frustrated with this one!

    Any help would be much appreciated!
    Attached Files Attached Files

  2. #2
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,806

    Re: Stuck on a large nested function...

    IN B9
    ="K"&SUBSTITUTE(LEFT(SUBSTITUTE(A9,"-","^",2),FIND("^",SUBSTITUTE(A9,"-","^",2))-1),"-","")&VLOOKUP(TRIM(RIGHT(SUBSTITUTE(A9,"-",REPT(" ",20)),3)),'Lookup Table'!$A$2:$B$98,2,FALSE)
    Last edited by martindwilson; 08-27-2011 at 07:32 PM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    MSO2007 on WinXP/MSO2000 on Win7/winXP
    Posts
    2,180

    Re: Stuck on a large nested function...

    Or maybe a UDF:
    Option Explicit
    
    Public Function ExtractCodes(ByVal OldString As String) As String
        Dim temparray   As Variant, _
            NewCode     As String, _
            tempstring  As String, _
            MaxIndex    As Long, _
            Ndx         As Long, _
            COLORTABLE  As Range
            
        Ndx = Sheets("lookup table").Cells(Rows.Count, "A").End(xlUp).Row
        Set COLORTABLE = Sheets("lookup table").Range("A2:B" & Ndx)
            
        Err.Clear
        temparray = Split(OldString, "-")
        MaxIndex = UBound(temparray)
        
        On Error Resume Next
        NewCode = WorksheetFunction.VLookup(temparray(MaxIndex), COLORTABLE, 2, False)
        If Err.Number = 1004 Then   'process zeros and "not found" errors
            If temparray(MaxIndex) = 0 Then
                NewCode = "WHI"
            ElseIf NewCode = "" Then
                NewCode = "XXX"
            End If
        End If
        
        For Ndx = 0 To MaxIndex - 1
            tempstring = tempstring & temparray(Ndx)
        Next Ndx
        tempstring = "K" & tempstring & NewCode
        ExtractCodes = tempstring
    End Function
    Attached Files Attached Files
    ---
    Ben Van Johnson

  4. #4
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,593

    Re: Stuck on a large nested function...

    Or this in B9, to allow for the cases where there are no middle characters (i.e. -??- )
    ="K"&SUBSTITUTE(LEFT(A9,LEN(A9)-LEN(TRIM(RIGHT(SUBSTITUTE(A9,"-",REPT(" ",10)),10)))-1),"-","")&INDEX('Lookup Table'!A:B,MATCH(TRIM(RIGHT(SUBSTITUTE(A9,"-",REPT(" ",10)),10)),'Lookup Table'!A:A,0),2)
    The new colour codes for S1,S2, & S5 are not in your lookup table and will return #N/A

    Hope this helps.
    Last edited by Marcol; 08-28-2011 at 06:11 AM. Reason: Corrected formula
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  5. #5
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,806

    Re: Stuck on a large nested function...

    good catch i never saw those like 4649-K4
    still it was late!
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  6. #6
    Registered User
    Join Date
    08-27-2011
    Location
    NH
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Stuck on a large nested function...

    That worked!!! Thank you so much!

+ 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.2.0