+ Reply to Thread
Results 1 to 9 of 9

User Defined Function

  1. #1
    Registered User
    Join Date
    09-15-2008
    Location
    Montana
    Posts
    11

    User Defined Function

    I am trying to write a UDF to handle a large trig formula.
    I will need to use the square root function (SQRT) during part of the calculation.
    I have been testing parts of the large formula. The SQRT function will not work. I get the #VALUE! message.
    I have also tried to test the SUM function and receive the same #VALUE! message. Here is the test code.

    Please Login or Register  to view this content.
    Any idea's on how to get standard functions, ie: SUM, SQRT, ACOS to work in a UDF?

    Thanks!
    Last edited by VBA Noob; 10-13-2008 at 02:01 PM. Reason: Added code tags as per forum rules

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387
    Frank e,


    Can we see the entire UDF that you are working on?


    Please Login or Register  to view this content.

    Have a great day,
    Stan

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Frank e,

    You can use most of Excel's worksheet functions in VBA. To use them in VBA they are called in a special way using the WorksheetFunction property. Unfortunately the SQRT function is not available in VBA. You have to write your own function, like this...
    Please Login or Register  to view this content.
    As for the others, like SUM, here is an example...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  4. #4
    Forum Contributor
    Join Date
    05-21-2004
    Location
    UK
    Posts
    136
    Quote Originally Posted by Leith Ross View Post
    Hello Frank e,

    You can use most of Excel's worksheet functions in VBA. To use them in VBA they are called in a special way using the WorksheetFunction property. Unfortunately the SQRT function is not available in VBA. You have to write your own function, like this...
    VBA has its own

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    05-21-2004
    Location
    UK
    Posts
    136
    And yours is a SQUARE function, not SQRT :-)

  6. #6
    Registered User
    Join Date
    09-15-2008
    Location
    Montana
    Posts
    11
    Thank you for the help. Not being able to use the square root function
    will make the large formula even larger. I will use x^0.5.
    Thanks again.

  7. #7
    Registered User
    Join Date
    09-15-2008
    Location
    Montana
    Posts
    11

    User defined Function

    Here is a copy of the code I'm using for my UDF.
    I am getting a #REF! message in the cell
    where I enter, =Add2(cell1,cell2,cell3,cell4).
    Any idea's on what I'm doing wrong?
    Thanks.
    Please Login or Register  to view this content.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Works fine for me:
    Please Login or Register  to view this content.
    A little clean-up:
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485
    SIN is another function that exists in VBA and not through the worksheetfunctions.
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Updation of UDF (User Defined Function)
    By vandanavai in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-26-2007, 06:16 AM
  2. using a user defined function
    By jayron in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2007, 06:04 PM
  3. Macro with user defined inputs
    By Nigel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-25-2007, 07:24 PM
  4. User Defined Function (UDF) in a shared workbook
    By arizenn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2007, 11:19 AM

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