+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: UDF for many if statements

  1. #16
    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

    Public Function DBValue( _
           ByVal M2 As Range, _
           ByVal Q2 As Range, _
           ByVal R2 As Range, _
           ByVal S2 As Range, _
           ByVal T2 As Range, _
           ByVal U2 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
        Dim vaItem
        Dim vaMatch
        Dim bMatched As Boolean
        Dim sFormula As String
        vecSun = Array()
    
        If InStr(1, M2.Value, "150") > 0 Then
            DBValue = "Toughened"
        ElseIf InStr(1, M2.Value, "130") + InStr(1, M2.Value, "210") + InStr(1, M2.Value, "999") > 0 Then
            DBValue = "Misc"
        ElseIf InStr(1, M2.Value, "800") > 0 Then
            DBValue = "Carriage"
        ElseIf 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
            DBValue = "Fire"
        ElseIf R2.Value Like "*Activ*" Or T2.Value Like "*Activ*" Or V2.Value Like "*Activ*" Then
            DBValue = "Activ"
        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 "*Planar*" Or T2.Value Like "*Planar*" Or V2.Value Like "*Planar*" Then
            DBValue = "Planar"
        Else
            For Each vaItem In Array(Q2.Value, S2.Value, U2.Value)
    
                If Application.CountIf(ProductGroup, vaItem) Then
                    bMatched = True
                    sFormula = Replace(Replace(Replace(FORMULA_MATCH_INDEX, _
                                                                            "<Q2>", vaItem), _
                                                                            "<productstart>", ProductGroup.Cells(1, 1).Address), _
                                                                            "<products>", ProductGroup.Address)
                    idxValue = Sheets("Product Group").Evaluate(sFormula)
                                                                            
                    DBValue = Application.Index(ProductGroup.Columns(1), idxValue)
                    Exit For
                End If
            Next vaItem
    
            If Not bMatched Then DBValue = AO2.Value & " Other"
        End If
    End Function
    You will need to adjust the formulas to add the M cell as the first argument.
    Good luck.

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

    Re: UDF for many if statements

    Thanks Rory - ill continue the testing...

    p.s. Appreciate all the time and help devoted on this!

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

    Re: UDF for many if statements

    Sorry, found something else I need to amend:

    ElseIf R2.Value Like "*Planar*" Or T2.Value Like "*Planar*" Or V2.Value Like "*Planar*" Then
            DBValue = "Planar
    I need to change this to look in N2 also so:
    ElseIf R2.Value Like "*Planar*" Or T2.Value Like "*Planar*" Or V2.Value Like "*Planar*"  Or N2.Value Like "*Planar*" Then
            DBValue = "Planar
    Do I need to add this to the list at the top e.g.
    ByVal N2 As Range, _

    and then change the formula?

  4. #19
    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

    Yes. (make sure you have the ranges in the right order)
    Good luck.

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

    Re: UDF for many if statements

    That done the trick (wanted to check before I did it)

    Ill continue to test...

+ 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