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!![]()
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
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
---
Ben Van Johnson
Or this in B9, to allow for the cases where there are no middle characters (i.e. -??- )
The new colour codes for S1,S2, & S5 are not in your lookup table and will return #N/A="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)
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.
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
That worked!!! Thank you so much!![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks