+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: UDF for many if statements

  1. #1
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    UDF for many if statements

    Hi,
    I have a database which I need to simplify into the following category groups:
    • Triple
    • Suncool
    • Fire
    • Lam
    • Activ
    • Planar
    • K
    • K S
    • Other

    However this would involve nesting the following IF formulas. In the past it was suggested I use a UDF, can anyone help with this based on the following rules please:
    1. If V2 contains any data then “Triple”
    2. If R2 or T2 or V2 contains *Suncool* or *Sun* then “Suncool”
    3. If R2 or T2 or V2 contains *pyrodur* or *Pyrostop*, then “Fire”
    4. If R2 or T2 or V2 contains *lam* or *phon* then “Lam”
    5. If R2 or T2 or V2 contains *Activ*, then “Activ”
    6. If R2 or T2 or V2 contains *Planar*, then “Planar”
    7. The tricky one is this one.. If any product codes in R2,T2, V2 are contained in the data table on the tab “product group” then apply the formula:

    =IF(COUNTIF('Product Group'!$M$5:$Q$8,Q2),INDEX('Product Group'!$M$5:$M$8,SUMPRODUCT(('Product Group'!$M$5:$Q$8=Q2)*(ROW('Product Group'!$M$5:$Q$8)-ROW('Product Group'!$M$5)+1))),AO2&" Other")
    However the above formula only looks in Q2 – this needs to be changed to look into R2,T2 and V2

    This will assign the product groups K and K S

    8. If none of the above apply then “Other”

    I have attached a sample file – in the actual file there are over 40000 entries to apply this to.

    When I used a UDF before for “Planar” for example I was given this code:
    If rRng.Value Like "*Planar*" Then
        fTMS = "Planar"
        Exit Function
    End If
    However I need it to look within 3 cells rather than one.

    The formula would need to go in column AL

    Any help is greatly appreciated, even if you can help with only one part

    Paul
    Attached Files Attached Files

  2. #2
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    MS Excel 2010
    Posts
    2,247

    Re: UDF for many if statements

    Public Function DBValue( _
        ByVal Q2 As Range, _
        ByVal R2 As Range, _
        ByVal T2 As Range, _
        ByVal V2 As Range, _
        ByVal AO2 As Range, _
        ByVal ProductGroup As Range) As String
    Const FORMULA_MATCH_INDEX As String = _
        "SumProduct((<products>=<Q2>)*" & _
        "(Row(<products>)-Row(<productstart>)+1)))"
    Dim idxValue As Long
    
        vecSun = Array()
        If V2.Value2 <> "" Then
            DBValue = "Triple"
        ElseIf R2.Value Like "*Sun*" Or T2.Value Like "*Sun*" Or V2.Value Like "*Sun*" Then
            DBValue = "Suncool"
        ElseIf (R2.Value Like "*pyrodur*" Or R2.Value Like "*Pyrostop*") Or _
            (T2.Value Like "*pyrodur*" Or T2.Value Like "*Pyrostop*") Or _
            (V2.Value Like "*pyrodur*" Or V2.Value Like "*Pyrostop*") Then
        ElseIf (R2.Value Like "*lam*" Or R2.Value Like "*phon*") Or _
            (T2.Value Like "*lam*" Or T2.Value Like "*phon*") Or _
            (V2.Value Like "*lam*" Or V2.Value Like "*phon*") Then
            DBValue = "Lam"
        ElseIf R2.Value Like "*Activ*" Or T2.Value Like "*Activ*" Or V2.Value Like "*Activ*" Then
            DBValue = "Activ"
        ElseIf R2.Value Like "*Planar*" Or T2.Value Like "*Planar*" Or V2.Value Like "*Planar*" Then
            DBValue = "Planar"
        ElseIf (IIf(R2.Value <> "", Application.CountIf(ProductGroup, Left$(R2.Value, 6)), 0)) + _
            (IIf(T2.Value <> "", Application.CountIf(ProductGroup, Left$(R2.Value, 6)), 0)) + _
            (IIf(V2.Value <> "", Application.CountIf(ProductGroup, Left$(V2.Value, 6)), 0)) > 0 Then
            
            If Application.CountIf(ProductGroup, Q2.Value) Then
            
                idxValue = Application.Evaluate(Replace(Replace(Replace(FORMULA_MATCH_INDEX, _
                                                                "<Q2>", Q2.Value), _
                                                        "<productstart>", ProductGroup.Cells(1, 1).Address), _
                                                "<products>", ProductGroup.Address))
                DBValue = Application.Index(ProductGroup.Columns(1), idxValue)
            Else
            
                DBValue = AO2.Value & " Other"
            End If
        End If
    End Function

  3. #3
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    Re: UDF for many if statements

    Hi Bob

    Thank you for replying to me and providing the code.

    I am just about to test this but fell at the first hurdle - how do I ativate the code? I inserted the formula: =DBValue() but I have a text box that comes up asking me for entries in Q2, R2, T2, V2, AO2 and Product group - what do I input in these? Attached Picture
    Attached Images Attached Images

  4. #4
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    Re: UDF for many if statements

    for this part:
    ElseIf (R2.Value Like "*pyrodur*" Or R2.Value Like "*Pyrostop*") Or _
            (T2.Value Like "*pyrodur*" Or T2.Value Like "*Pyrostop*") Or _
            (V2.Value Like "*pyrodur*" Or V2.Value Like "*Pyrostop*") Then
    would I need to add in?:
    HTML Code: 
    DBValue = "Fire"

  5. #5
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    Re: UDF for many if statements

    Anyone know how i can get this to work?

  6. #6
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: UDF for many if statements

    Bob did try and make the parameters fairly self-evident - something like

    =DBValue(Q2,R2,T2,V2,AO2,'Product Group'!$M$5:$Q$8)
    Good luck.

  7. #7
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    MS Excel 2010
    Posts
    2,247

    Re: UDF for many if statements

    Quote Originally Posted by OnErrorGoto0 View Post
    Bob did try and make the parameters fairly self-evident - something like

    =DBValue(Q2,R2,T2,V2,AO2,'Product Group'!$M$5:$Q$8)
    I even tried making the parameters self-documenting to help

  8. #8
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    Re: UDF for many if statements

    Hi,

    That simple but yet I still couldnt figure it out!

    I have started testing this and I have a problem with it not picking up products which appear in the product group table, this refers to this code:

    ElseIf (IIf(R2.Value <> "", Application.CountIf(ProductGroup, Left$(R2.Value, 6)), 0)) + _
            (IIf(T2.Value <> "", Application.CountIf(ProductGroup, Left$(R2.Value, 6)), 0)) + _
            (IIf(V2.Value <> "", Application.CountIf(ProductGroup, Left$(V2.Value, 6)), 0)) > 0 Then
            
            If Application.CountIf(ProductGroup, Q2.Value) Then
            
                idxValue = Application.Evaluate(Replace(Replace(Replace(FORMULA_MATCH_INDEX, _
                                                                "<Q2>", Q2.Value), _
                                                        "<productstart>", ProductGroup.Cells(1, 1).Address), _
                                                "<products>", ProductGroup.Address))
                DBValue = Application.Index(ProductGroup.Columns(1), idxValue)
            Else
            
                DBValue = AO2.Value & " Other"
    Can anyone see why this isnt working?

    I have uploaded the file using the code supplied in column AL, the highlighted red entries are the cells which are not working correctly.

    Regards

    Paul
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: UDF for many if statements

    Where does column S come into your description of what is supposed to happen?
    Also, your product codes in R, T and V do not appear in the table, so Q is never evaluated.

    I think perhaps you need to redo your description of what you are expecting.
    Good luck.

  10. #10
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    Re: UDF for many if statements

    Hi,

    What this part of the formula would need to do is look in cells Q2, S2 and U2 and if any of the products appear within the product table on "product group" tab then return the category in column M.
    e.g. on the previously attached file in cell Q3, this appears on the product group tab in cell N5 so "4mm K" should be returned.

    If it doesn’t appear on the product group tab then cell AO2 and “Other” should be returned

    I can see the formula is looking in columns R, T, V which is probably my fault in explaining it previously. I am sure it isnt as simple as changing R2 to Q2, T2 to S2 and V2 to U2.

    Can anyone help with the amendment?

  11. #11
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: UDF for many if statements

    Well the fact that part of the formula works, makes me think you still need to be looking at R, T and V, but your original post said for part 7:
    "If any product codes in R2,T2, V2 are contained in the data table on the tab “product group” then apply the formula..."
    clearly that is not the case, so is it just that part where you need to refer to Q, S and U instead?
    Good luck.

  12. #12
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    Re: UDF for many if statements

    Yes, most of the formula works - it is only when it comes down to this part which is incorrect. All code above this can be left the same as it is working.

    The statement below by me is wrong:
    "If any product codes in R2,T2, V2 are contained in the data table on the tab “product group” then apply the formula..."

    This should be Q2, S2 and U2

  13. #13
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: UDF for many if statements

    Try the attached. I have edited the code to take two more range arguments and process a little differently, and adjusted the formulas in the cells.
    Attached Files Attached Files
    Good luck.

  14. #14
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    Re: UDF for many if statements

    ill test this now!!

  15. #15
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel 2007
    Posts
    550

    Re: UDF for many if statements

    Hi,

    Still testing this but found something that I need including if possible:
    If cell M2 contains "150" Then "Toughened"
    If cell M2 contains "130" or or "210" or "999" Then "Misc"
    If cell M2 contains "800" then "Carriage"


    I would need this to go near the top of the code before tripple if possible. Can you help?

+ 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