Link to home
Start Free TrialLog in
Avatar of Alex Campbell
Alex CampbellFlag for United States of America

asked on

How to turn this IF statement into a UDF?

While I know this is a simple IF statement, I am learning UDFs so I wanted to see how it is done in this case.

In the attached worksheet, this is the formula for the first row:
=IF(G3="N",A3&"-"&B3&"-"&C3,D3&"."&E3)

User generated image
Options.xlsx
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

public function MyFirstFunction(rng as range) as string
    if rng.value="N" then
        MyFirstFunction = range("A3").value & "-" & range("B3").value & "-" & range("C3").value 
    else
        MyFirstFunction = range("D3").value & "." & range("E3").value 
    end if
end function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
Norie

Perhaps.
Function MyJoinUDF(rng As Range) As String

    If rng.Value = "N" Then
        MyJoinUDF = Join(Application.Transpose(Application.Transpose(rng.Offset(, -6).Resize(, 3).Value)), "-")
    Else
        MyJoinUDF = rng.Offset(, -3).Value & "." & rng.Offset(, -2).Value
    End If

End Function

Open in new window

Here is an article of mine that you might find useful.
Avatar of Alex Campbell

ASKER

Great. Thanks