Consulting

Results 1 to 19 of 19

Thread: Solved: Formula to help identify specific words in titles?

  1. #1

    Solved: Formula to help identify specific words in titles?

    I am looking for the best way to identify colors in titles and then if a color is identified put that color name into a separate column. Multiple colors could be included in the same cell, separated by a comma. If no color exists then the cell if left blank.

    For example:
    Product Name #1: Cosmos Green & White case for Samsung Gravity
    Product Name #2: Cosmos Green case for Samsung Gravity

    (List of colors: white, blue, green, black, yellow, red, purple)


    A1: Product Name | A2:Colors
    Product #1 name | Green , White
    Product #2 name | Green

    Any ideas for how best to accomplish this?

  2. #2
    Paste this code in a module and then use the UDF getcolors() in the column you want the colors to appear in

    [VBA]Public Function IsInArray(arr As Variant, valueToFind As Variant) As Boolean
    IsInArray = (UBound(Filter(arr, valueToFind)) > -1)
    End Function

    Public Function getColors(strprodname As String) As String
    Dim arrColors()
    Dim strColors As String
    Dim x As Variant
    Dim n As Integer

    arrColors = Array("white", "blue", "green", "black", "yellow", "red", "purple")

    x = Split(strprodname, " ")
    For n = 0 To UBound(x)
    If IsInArray(arrColors, LCase(x(n))) Then
    strColors = strColors & x(n) & " , "
    End If
    Next n

    If Len(strColors) > 0 Then
    strColors = Trim(Left(strColors, InStrRev(strColors, ",") - 1))
    End If

    getColors = strColors
    End Function[/VBA]

    for example, in cell a2:


    [VBA]=getcolors(a1)[/VBA]
    Last edited by Shred Dude; 06-02-2010 at 08:54 PM.

  3. #3

    worked great, one remaining quesitons tho....

    That worked really well! Thanks.
    Actually couple of interesting things that popped up when I ran it:

    1) Celtic Cross on Black designed skin for Amazon Kindle 2 returned the result:
    on , Black

    2) da Vinci - Sketch of a roaring lion designed skin for Amazon Kindle 2 returned the result: a

    3) Hello Kitty - On a Cloud designed skin for Amazon Kindle 2 returned the result: On , a

    Not sure if there's a way to get rid of those unwanted results?
    Thanks again for the help. You are one smart cookie!

  4. #4
    I can't explain the "On" making it into the results unless you've added a color to the arrColors variable that contains the letters "on" in sequence. Blonde?

    This is a bit sloppy, assuming you'll have no single or two lettered colors, we could go with this:

    Public Function getColors(strprodname As String) As String
    Dim arrColors()
    Dim strColors As String
    Dim x As Variant
    Dim n As Integer
    
    arrColors = Array("white", "blue", "green", "black", "yellow", "red", "purple")
    
    x = Split(strprodname, " ")
    For n = 0 To UBound(x)
        If Len(x(n)) > 2 Then
        If IsInArray(arrColors, LCase(x(n))) Then
            strColors = strColors & x(n) & " , "
        End If
        End If
    Next n
    
    If Len(strColors) > 0 Then
    strColors = Trim(Left(strColors, InStrRev(strColors, ",") - 1))
    End If
    
    getColors = strColors
    End Function
    Hope that helps...

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A slightly different function?
    [VBA]
    Public Function IsInArray(arr As Variant, valueToFind As Variant) As Boolean
    IsInArray = Not IsError(Application.Match(valueToFind, arr, 0))
    End Function

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    That worked great! Thanks for the help.

  7. #7
    And they said MD is just another pretty face . . .

    Sid

  8. #8
    yes, strikingly handsome in addition to brilliant!

  9. #9
    One more question for you brilliant minds
    what if I wanted to reference a second worksheet of colors instead of listing them individually in the macro? My list of colors could grow to over 40 and there's some mispellings I am trying to include in the results.

    Let's assume the ref worksheet name is ColorsList.
    Also, For each color, I'd like to include some possible mispellings. Solet's say there's up to 10 columns total:

    A | B | C | D | E | F | G | H | I | J |

    1) Blue | Bleu | Blu |
    2) Navy Blue | Nave Blue | Navy Bleu | Navi Blue | navy blue
    3) Forest Green | Forst Green | Forest Grene | Forest Greeen | forestgreen

    So now, if any color is a match within each row, all keywords in that row get entered into the cell. forest green, forst green, forest grene, forest green, forestgreen, green, forest, greeen, forst

    Each row may contain different number of additional keywords as shown below.

    Is this even possible to do in a macro? Maybe I have to break out the 2 word colors into individual columns first before those can get entered as well?

  10. #10
    I will definitely send a beer (or 6) to the guy who can help me with this one!

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post some sample data.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    yes, here's a sample workbook for you. I left the previous macro from above in Column C and added an example of how the additional keywords should ideally look in Column D.

    Really not sure how to work this into a macro. doesn't have to be the same one, could be a new one altogether. The key to what I'm trying to do is that if any color variation matches what's in the product name, then all keywords get combined into the result in Column D.

    Thanks a million for taking a look!

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Best guess!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    unfortunately i'm just seeing #VALUE! errors.

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try downloading again, I tidied up the code.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  16. #16
    unfortunately same thing. attached the screen shot

  17. #17
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You should see this. Sorry, I need to leave this for now. Away til tuesday
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  18. #18
    ok. thanks for the help.
    is there a quick way to "refresh" or "restart" the macro?

  19. #19
    We'll I got it to work, but it was very strange. all i did was change =getcolors(B2) to =getcolors(B1) and changed it back to =getcolors(B2)
    and that seemed to "refresh" it. so it looks great now!

Posting Permissions

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