+ Reply to Thread
Results 1 to 10 of 10

UDfs to react similarly to built-in functions pertaining to spreadsheet usability

  1. #1
    Registered User
    Join Date
    10-26-2012
    Location
    Weast
    MS-Off Ver
    Excel 2003
    Posts
    6

    UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    I'm trying to figure out a template for my functions so that they may be handled (stretched and expanded on spreadsheet) the same as built-in functions such as "sin()" for "Enter" and "Ctrl+Shift+Enter" to handle array outputs to pre-highlighted cells.

    I've only ever created functions where it outputted only to that specific cell that it was called in. Such as when I have a column of x values A1 through A5. I put f(x) in column B and calculate f(A1), f(A2) ... f(A5). Works no problem. But, if I name array spam = [A1 A2 A3 A4 A5] and enter f(spam) in column B, it does not work. Instead I get error of type #VALUE. This works for built in Excel functions like sin, cos, log, etc but I would like to know how to get it to work in my user defined function.

    I borrowed the following from another post on this forum... http://www.excelforum.com/excel-prog...-function.html

    It's close to what I want to achieve, but...
    1) I'd rather not have to pass 2 variables, 1 for the length of the input array and 1 for the range of the input array.
    2) The function operation can't be expanded/stretched down its column to calculate the output of its respective input column for "Enter"

    Please Login or Register  to view this content.
    Also, one thing I don't understand in Excel is how "sin(A1:A9) + Enter" is only able to carry out the operation within same rows as the input, 1 to 9 in this case. Otherwise, #VALUE will be returned if sin(A1:A9) + Enter is carried out beyond row 9.

    Thanks a lot for your time.

  2. #2
    Registered User
    Join Date
    10-26-2012
    Location
    Weast
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    Oh I figured out to use list.Rows.Count to get 'N' the number of elements in the input range, so that eliminates my 2 problems.

    However!
    "=Fjunk2(spam)" still won't work the same as "sin(spam)"

    =Fjunk2(spam) will only output the calculated answer for the first element from the input column in all the rows 1-9.
    =sin(spam) will output the calculated answer for its respective element from the input column for the rows 1-9.
    *spam = A1:A9

    I think if I get past this hurdle I'll have a basic template for functions to handle simple algebraic calculations that can be applied to the spreadsheet the same as the built-in functions.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    10-26-2012
    Location
    Weast
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    I figured out that I have to return a range instead of an array.
    However, I keep getting the circular reference notification and am not getting the results that I want.
    Is the problem stemming from how I am initializing the range object x?
    I think "set x = Selection" is causing the problem.
    Therefore, right now I'm trying to figure out other ways to "set x".
    I'd appreciate any comments.

    Thank you.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-26-2012
    Location
    Weast
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    I also tried the following, but it runs into the circular reference notification as well.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    Is this a function you intend to use on a worksheet?
    This function returns a range, It needs to return value if used on a sheet.
    You are specifying a range with the 'list' variable and attempting to change it's values using a function.
    You cannot use a function to change values in other cells
    Last edited by SuitedAces; 11-05-2012 at 02:05 AM.

  6. #6
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    Maybe this will help, I'm not sure this is the only approach but it works as intended

    Please Login or Register  to view this content.
    Last edited by SuitedAces; 11-05-2012 at 01:52 AM.

  7. #7
    Registered User
    Join Date
    10-26-2012
    Location
    Weast
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    Thanks for your replies SuitedAces!

    I see that I was misunderstanding the use of range variable by treating it as an array that I could load.
    If functions can't change cell values, and functions can't call subs that change cell values... are there any ways to change cell values in a manner that can be called from a cell in a worksheet?

    I tried your code and was only able to make it work in the following fashion. Does arr = rng work? Nice use of Lbound and Ubound. I didn't know it could be used to get the dimensions of a multidimensional array.

    It's just that, rng(A1:A9) + Enter <> sin(A1:A9) + Enter, in terms of which element in A1:A9 that function processes.
    Although, rng(A1:A9) + CSE == sin(A1:A9) + CSE, in that both process and spit out arrays properly.

    I mean, it's not a big deal. Normally one wouldn't use a function by putting inputting an array without CSE.
    I just feel like my function would be more complete if it handled exactly as a built-in function.

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    You're confusing me slightly, there's a gap in the terminology.
    But reading between the lines it seems like you're asking why you cannot use a UDF as an array function.
    The example I gave you can be.
    Make sure the function is entered in a standard module (not a sheet or workbook module)
    Select a range which is equal in dimension of the range that you are using for the function argument.
    Enter the function in the formula bar then hit control shift + enter.

    So let's say you want cube for A1:A3 in D1:D3, first select D1:D3
    type =cube( in the formula bar then select A1:A3, type ), enter the formula with control shift + enter.

    All you need to do to get your function working properly is to replace my math operation your math or string operation.
    Last edited by SuitedAces; 11-05-2012 at 09:01 PM.

  9. #9
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    My apologies I never checked this function against a single cell as in, =cube(A1)
    This generates a #VALUE! error.
    And I never checked it for entering a value directly as in, =cube(3)
    Here is a corrected version...
    Please Login or Register  to view this content.
    Using that same stem here is another simple function
    Please Login or Register  to view this content.
    Last edited by SuitedAces; 11-05-2012 at 10:37 PM.

  10. #10
    Registered User
    Join Date
    10-26-2012
    Location
    Weast
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: UDfs to react similarly to built-in functions pertaining to spreadsheet usability

    It's been a while, sorry I haven't responded sooner. I took time off to focus on my studies.
    Thanks a lot SuitedAces for all your input and time. However, it wasn't quite what I was looking for.
    Since I finally got back to working on this, I got help through other means and was finally able to solve this problem.
    The following is coded to mimic the response of built-in functions such as sin().
    If anyone knows of a simpler way to do this, perhaps by utilizing the nature of excel better. Please let me know.
    Thank you.

    Please Login or Register  to view this content.

+ 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