+ Reply to Thread
Results 1 to 5 of 5

How can I create a user defined function in excel?

  1. #1
    Martinj
    Guest

    How can I create a user defined function in excel?

    If I have a equation eg: =(A1+B1*if(t10=TRUE;1;-1)), how can I define it to
    a Myfunction (A1;B1;T10)?


  2. #2
    Bernard Liengme
    Guest

    Re: How can I create a user defined function in excel?

    Use Alt+F11 to open VB Editor
    Use command Insert | Module
    Copy this to the newly open module sheet
    Function Myfunction(x, y, z)
    If z Then
    multi = 1
    Else
    multi = -1
    End If
    Myfunction = x + y * multi
    End Function
    Return to Excel and type in any cell =Myfunction(A1,B1, T10)

    But why not use
    Function Myfunction(x, y, z)
    If z Then
    Myfunction = x + y
    Else
    Myfunction = x - y
    End If
    End Function

    best wishes

    --
    Bernard V Liengme
    www.stfx.ca/people/bliengme
    remove caps from email

    "Martinj" <[email protected]> wrote in message
    news:[email protected]...
    > If I have a equation eg: =(A1+B1*if(t10=TRUE;1;-1)), how can I define it
    > to
    > a Myfunction (A1;B1;T10)?
    >




  3. #3
    Tushar Mehta
    Guest

    Re: How can I create a user defined function in excel?

    Or what I would use:

    MyFunc = x + IIf(z, y, -y)
    MyFunc = x + y * IIf(z, 1, -1)

    To the point, no code duplication, and easier to maintain.

    It would be even more useful if VB terminated its evaluation as soon as
    the result was determined.

    --
    Regards,

    Tushar Mehta
    www.tushar-mehta.com
    Excel, PowerPoint, and VBA add-ins, tutorials
    Custom MS Office productivity solutions

    In article <[email protected]>,
    [email protected] says...
    > Use Alt+F11 to open VB Editor
    > Use command Insert | Module
    > Copy this to the newly open module sheet
    > Function Myfunction(x, y, z)
    > If z Then
    > multi = 1
    > Else
    > multi = -1
    > End If
    > Myfunction = x + y * multi
    > End Function
    > Return to Excel and type in any cell =Myfunction(A1,B1, T10)
    >
    > But why not use
    > Function Myfunction(x, y, z)
    > If z Then
    > Myfunction = x + y
    > Else
    > Myfunction = x - y
    > End If
    > End Function
    >
    > best wishes
    >
    >


  4. #4
    Registered User
    Join Date
    08-11-2005
    Location
    Netherlands Waddinxveen
    Posts
    81
    Some further comment function where made to return a value
    subs don't so write subs like:
    Please Login or Register  to view this content.
    and function

    Please Login or Register  to view this content.
    The function will return the smallest of the 2 or three numbers or cell value's

  5. #5
    Bernard Liengme
    Guest

    Re: How can I create a user defined function in excel?

    I have never liked the IIF construct!
    --
    Bernard V Liengme
    www.stfx.ca/people/bliengme
    remove caps from email

    "Tushar Mehta" <[email protected]> wrote in message
    news:[email protected]...
    > Or what I would use:
    >
    > MyFunc = x + IIf(z, y, -y)
    > MyFunc = x + y * IIf(z, 1, -1)
    >
    > To the point, no code duplication, and easier to maintain.
    >
    > It would be even more useful if VB terminated its evaluation as soon as
    > the result was determined.
    >
    > --
    > Regards,
    >
    > Tushar Mehta
    > www.tushar-mehta.com
    > Excel, PowerPoint, and VBA add-ins, tutorials
    > Custom MS Office productivity solutions
    >
    > In article <[email protected]>,
    > [email protected] says...
    >> Use Alt+F11 to open VB Editor
    >> Use command Insert | Module
    >> Copy this to the newly open module sheet
    >> Function Myfunction(x, y, z)
    >> If z Then
    >> multi = 1
    >> Else
    >> multi = -1
    >> End If
    >> Myfunction = x + y * multi
    >> End Function
    >> Return to Excel and type in any cell =Myfunction(A1,B1, T10)
    >>
    >> But why not use
    >> Function Myfunction(x, y, z)
    >> If z Then
    >> Myfunction = x + y
    >> Else
    >> Myfunction = x - y
    >> End If
    >> End Function
    >>
    >> best wishes
    >>
    >>




+ 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.6.0 RC 1