+ Reply to Thread
Results 1 to 9 of 9

VBA functions iin array formulas

  1. #1
    Registered User
    Join Date
    04-05-2012
    Location
    Bremen, Germany
    MS-Off Ver
    Excel 2010
    Posts
    4

    VBA functions iin array formulas

    Hi,

    I have a question:

    I can use for example the SIN-function for array formulas as in {=SIN(A1:A5)}. The original SIN-function takes one argument and returns a number.
    Now, if I have a user defined function TEST, which takes one argument and returns a number, I cannot use this function in the same manner.
    Is there a possibility to write the function, such that it can be used like (for example) the SIN-function?

    Cord

  2. #2
    Valued Forum Contributor
    Join Date
    03-14-2012
    Location
    Arizona USA
    MS-Off Ver
    Excel 2000/2007
    Posts
    408

    Re: VBA functions iin array formulas

    If you are asking, can I make a custom worksheet function? The answer is YES!

    To do this
    1. Enter the VB editor.
    2. Add a module to the project.
    3. See the example below.

    Please Login or Register  to view this content.
    Inside a cell, you would type

    Please Login or Register  to view this content.
    In this example, the function takes the value(single data type) that is in cell A1 and multiplies it by 3.14. The result(single data type) is returned to the cell that contains the custom worksheet function.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA functions iin array formulas

    Dennis,

    While your function will certainly work as a UDF, all numbers on a worksheet are Doubles, so forcing a conversion to Single, computing the result as a Single, and then converting back to a Double on the worksheet is unnecessary overhead.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    04-05-2012
    Location
    Bremen, Germany
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: VBA functions iin array formulas

    Thank You, but Your answer does not answer my question. I want such a function to use as an array formula. In the manner as I could do it for example with SIN (or COS, or SQRT ...).
    Last edited by davesexcel; 04-05-2012 at 11:18 PM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA functions iin array formulas

    Please Login or Register  to view this content.
    Last edited by shg; 04-05-2012 at 01:16 PM.

  6. #6
    Registered User
    Join Date
    04-05-2012
    Location
    Bremen, Germany
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: VBA functions iin array formulas

    Thank You! So I have to manage the subtleties myself. I initially conjectured that Excel does this for me: calling my function for every element
    in the range (or array) and manufacturing an result array of the same dimensions from all the return values. It would have been an elegant solution, wouldn't it?
    Last edited by davesexcel; 04-05-2012 at 11:17 PM.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA functions iin array formulas

    Yes, though you could write a generalized function to convert the input to a 2D array to simplify the code for any particular array-compatible UDF.

    There's no need to quote my posts back to me -- I know what I wrote.

  8. #8
    Registered User
    Join Date
    04-05-2012
    Location
    Bremen, Germany
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: VBA functions iin array formulas

    Sorry, shg

    it was not my intention to get on your nerves!

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA functions iin array formulas

    My nerves are fine. It's just unnecessary clutter.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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