+ Reply to Thread
Results 1 to 23 of 23

Help with user defined function

  1. #1
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Help with user defined function

    I have written a user defined function and am having two issues. The function works fine when x is an even integer but returns #VALUE! when x is an odd integer. I am thinking the issue is with the way I am using my If statements, but I am not sure. My second issue is that I want the value to x to be a value that is pulled from a cell on another sheet. I am confused on how to do this, not sure if I ned to write a sub function or if there is some existing code within VBA that will allow me to do this.

    Here is my code:

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor adyteo's Avatar
    Join Date
    01-10-2013
    Location
    Calgary
    MS-Off Ver
    Excel 2010
    Posts
    540

    Re: Help with user defined function

    firstly, correct the code (always compile your code before running it)
    Please Login or Register  to view this content.
    is wrong. it should be:
    Please Login or Register  to view this content.
    Click on the star if you think I helped you

  3. #3
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    Thanks for catching that adyteo. Ironically, that spelling mistake didn't keep the "even" part of my code from running correctly. I am still not getting the #VALUE! result when x is an odd integer.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,840

    Re: Help with user defined function

    One of the first debugging skills I learned and that I probably use the most is the ability to step through a function one step at a time: http://office.microsoft.com/en-us/su...819.aspx?CTT=1 My first suggestion in debugging something like this is to set a breakpoint somewhere early in the function (I often use the function statement itself), then step through the code. This will allow you to identify which statement fails to execute, or which values are present in variables that can cause errors, etc.

    When I stepped through it, the first error I found (after the typo mentioned above) was with the use of the .trunc() worksheet function. According to this list of worksheet functions available to VBA, trunc is not on the list: http://msdn.microsoft.com/en-us/libr...ice.15%29.aspx You will probably need to search through the available rounding functions in VBA to get the effect you want here.

    See where that gets you.

    My second issue is that I want the value to x to be a value that is pulled from a cell on another sheet.
    For this, add another argument to the function statement so you can pass this second argument to the function.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Valued Forum Contributor adyteo's Avatar
    Join Date
    01-10-2013
    Location
    Calgary
    MS-Off Ver
    Excel 2010
    Posts
    540

    Re: Help with user defined function

    the function Trunc does not exist in VBA nor in VBA.Worksheetfunctions.

    What do you really want to do with your code?

  6. #6
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    MrShorty, thanks for your input. I am going to see how I can round down to achieve my result. Question: if I declare x to be an integer and perform an operation on x that returns a float, does the truncated value get stored to x? For example: Dim
    Please Login or Register  to view this content.
    Since x is an integer, will it equal 3 or 4?

  7. #7
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    Not to get off topic, but I am attempting to step through the code line-by-line and when I click "Step Into" or use the keyboard shortcut, nothing happens.

  8. #8
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    adyteo: The purpose of my code is to take a "ranking" and apply a value to it, which will be dependent on the total number of items being ranked.

    For example, if I have 5 items ranked 1 through 5, I want to apply a value to it. The middle ranked value will always be zero and values will increase or decrease by 2 depending on whether the ranking is higher or lower, respectively.

    Item Rank Value
    Item A 1 4
    Item B 2 2
    Item C 3 0
    Item D 4 -2
    Item E 5 -4

  9. #9
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    I changed my code since I can't use the trunc function. Instead I am using INT() to achieve my desired value. However, changing this still does not produce my desired results and I am having issues using the debugging tools. I am unable to step through my code. I am running Mac Excel 2011.
    Last edited by mbxs3; 04-16-2014 at 11:02 AM. Reason: Autocorrected something that didn't need autocorrecting

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,840

    Re: Help with user defined function

    Quote Originally Posted by mbxs3 View Post
    Not to get off topic, but I am attempting to step through the code line-by-line and when I click "Step Into" or use the keyboard shortcut, nothing happens.
    With UDF's, I have found that the "step into" command fails -- because this command has no way to assign values to the required arguments of the function. The best way I have found to enter debug mode with UDF's is as I described above, set a breakpoint, then call the function from the spreadsheet (just like you are doing now). When the code execution encounters the breakpoint, it pauses, enters break mode, and you can then use the other debugging tools to determine what is going on. (in the link I added above, study the section "setting a breakpoint" for detailed instructions).

  11. #11
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    Will do, thanks MrShorty.

  12. #12
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    Ok, so my code started working, although I don't think I changed anything major. Im starting to think my Mac Excel version is glitchy. I am still having one issue which I will explain below my code. Here is my updated UDF code:

    Please Login or Register  to view this content.
    My issue occurs when the value for "slots" is 5, 9, 13, …etc. I never get the return value of 2. Instead, the item with the ranking that should produce the value 2 produces the value of 0. However, if the "slots" value is odd integers 3, 7, 11, ... etc. it works fine.
    Last edited by mbxs3; 04-16-2014 at 01:16 PM. Reason: edited code changes

  13. #13
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    To expand on what I mean…here is a table where I have an Item, its rank, and its value (where priceFactor is performed).

    My table for 5 items should look like this:

    Item Rank Value
    A 1 4
    B 2 2
    C 3 0
    D 4 -2
    E 5 -4

    However I am getting a table with this result:

    Item Rank Value
    A 1 4
    B 2 0
    C 3 0
    D 4 -2
    E 5 -4

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

    Re: Help with user defined function

    Perhaps instead

    A
    B
    C
    1
    Rank
    Price Factor
    2
    1
    4
    B2: =Pricefactor(A2, 5)
    3
    2
    2
    4
    3
    0
    5
    4
    -2
    6
    5
    -4
    7
    8
    1
    5
    B8: =Pricefactor(A8, 6)
    9
    2
    3
    10
    3
    1
    11
    4
    -1
    12
    5
    -3
    13
    6
    -5


    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  15. #15
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,840

    Re: Help with user defined function

    I know I get to sounding like a broken record (for those who can remember what vinyl records are), but I think you should be able to see where the problem occurs if you step through the function. You would see that this result is caused when rank = zero.

    The reason that rank and zero are sometimes equal when you may not expect them to be is that VBA's round() function uses banker's rounding (rounding to the nearest even number) when you get an even 0.5. So 5/2=2.5 rounds to 2 (not 3 the way many of us are used to seeing). http://support.microsoft.com/kb/194983/en-gb You will probably need to think through your code and see what this means for your code and how you approach the arithmetic.

  16. #16
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    Thanks MrShorty, I was using breakpoints and stepping through the code but I wasn't able to see that the issue was occurring when rank = zero. I think I solved my issue by instead of using Round(), I used Int(x / 2) + 1. Thanks for all of the help!

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

    Re: Help with user defined function

    I feel so ... ignored

  18. #18
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    I apologize shg. I was looking at your reply when MrShorty replied. My initial issue with your code was that it did not produce the desired results when iSlot = 6. I always want the value to increase and decrease from zero by 2.

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

    Re: Help with user defined function

    So what should the results be for 6 slots?

  20. #20
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    Item Rank Price Factor
    A 1 4
    B 2 2
    C 3 0
    D 4 0
    E 5 -2
    F 6 -4

    Sorry for being unclear. In the case of even slot numbers, there ends up being two price factors with a value of zero.

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

    Re: Help with user defined function

    A
    B
    C
    1
    Rank
    Price Factor
    2
    1
    4
    B2: =Pricefactor(A2, 5)
    3
    2
    2
    4
    3
    0
    5
    4
    -2
    6
    5
    -4
    7
    8
    1
    4
    B8: =Pricefactor(A8, 6)
    9
    2
    2
    10
    3
    0
    11
    4
    0
    12
    5
    -2
    13
    6
    -4


    Please Login or Register  to view this content.
    Last edited by shg; 04-17-2014 at 11:41 AM.

  22. #22
    Registered User
    Join Date
    11-18-2012
    Location
    United States
    MS-Off Ver
    Excel 2010 (MAC)
    Posts
    29

    Re: Help with user defined function

    Wow! Very nice and clean. I went around my butt to get to my elbow.

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

    Re: Help with user defined function

    You're welcome.

+ 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. [SOLVED] User defined function returns an error on a standard function used in it.
    By pb48 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-23-2013, 01:35 PM
  2. User Defined Function Add-ins
    By ot070707 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-04-2011, 11:26 AM
  3. using a user defined function
    By jayron in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2007, 06:04 PM
  4. [SOLVED] Excel - User Defined Function Error: This function takes no argume
    By BruceInCalgary in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-18-2006, 04:05 PM
  5. Replies: 0
    Last Post: 06-20-2006, 10:55 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