+ Reply to Thread
Results 1 to 16 of 16

Problem with user defined function

  1. #1
    Forum Contributor Mayda89's Avatar
    Join Date
    09-13-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    192

    Problem with user defined function

    Hey Guys,

    I am having a problem with my user defined function.
    The weird thing is, it works fine, until I open another excel file.
    After opening a new excel file, the formulas in the original file start returning #Value errors; these errors disappear when I just re-enter the formula to those cells.

    The code of the function is below.

    P.S: The code of the function is stored in the workbook, not in the personal macroworkbook or an add-in. And please note that I want to keep it this way.

    Thank you very much for your help.

    Please Login or Register  to view this content.

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

    Re: Problem with user defined function

    You are defining and setting the value of "MyRange" inside of the function rather than passing this value/reference through the argument list. The reference for MyRange [Range("Table4")] is also unqualified, so it will refer to the activesheet/workbook (even if that is not the sheet/workbook calling the UDF). Perhaps the 2nd workbook does not have a range named "Table4" -- which will cause an error.

    Correcting this will require you to define MyRange more carefully. IMO, you should strongly consider passing MyRange to the function through the argument list, just like you are doing with the other three ranges.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Contributor Mayda89's Avatar
    Join Date
    09-13-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: Problem with user defined function

    Hi MrShorty!
    Thank you very much for the reply!
    I think your suggestion will help me solve this problem!

    Passing the MyRange through the arguments is impractical, because since this range will always be the same range, there is no point in making the user select this range.
    So I prefer to qualify the reference for Range("Table4").
    I tried to do it in several different ways, but I couldn't make it work.

    A)
    Please Login or Register  to view this content.
    B)
    Please Login or Register  to view this content.
    C)
    Please Login or Register  to view this content.
    D)
    Please Login or Register  to view this content.
    E)
    Please Login or Register  to view this content.
    F)
    Please Login or Register  to view this content.
    G)
    Please Login or Register  to view this content.
    H)
    Please Login or Register  to view this content.
    I)
    Please Login or Register  to view this content.
    None of these work :S

    By the way, I would prefer a solution where I would use the codename of the workbooks and worksheets if that is possible.

    Note That "Otomatik Portföy" is the name of the workbook,
    "wOtomatikPortföy" is the codename of the workbook,
    "Liste" is the name of the sheet where the Table4 is located,
    "ShtListe" is the codename of that sheet.


    Thank you very much for your help!
    If I have helped, Don't forget to add to my reputation (click on the star icon below the post).
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved).
    Use code tags when posting your code.

  4. #4
    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: Problem with user defined function

    there is no point in making the user select this range.
    Unless you'd like the UDF to recalculate automatically when that range is changed.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Contributor Mayda89's Avatar
    Join Date
    09-13-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: Problem with user defined function

    Quote Originally Posted by shg View Post
    Unless you'd like the UDF to recalculate automatically when that range is changed.
    Well, that range is never going to change

  6. #6
    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: Problem with user defined function

    Well, that range is never going to change
    Maybe not; it's still atrocious coding practice.

    A range is an object variable, and object variables require Set:

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor Mayda89's Avatar
    Join Date
    09-13-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: Problem with user defined function

    MyRange is not a range, it is an array.
    You can see this, since it is declared as variant, not an array.
    And you can't use "set" on arrays.

    Please note that I have to keep it as an array.
    Last edited by Mayda89; 09-19-2014 at 01:27 AM.

  8. #8
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Problem with user defined function

    If range values will not change why do you use worksheetfunction.Count instead of constant value?
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  9. #9
    Forum Contributor Mayda89's Avatar
    Join Date
    09-13-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: Problem with user defined function

    The range is a table.
    So the table can grow, so that's why the number of elements in it can change.
    However, since I'm referring to the table with it's name, I will never need to update this reference.
    Why are you questioning me instead of trying to help me?

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem with user defined function

    I think you'll find that they are trying to help you. You might want to be a little more appreciative.

    For a UDF you should pass all ranges as arguments. If you don't, you have to make your function volatile otherwise when your referenced table expands, your functions won't recalculate automatically and properly qualify the ranges, as mentioned. In this case you should be able to use:
    Please Login or Register  to view this content.
    but it is still better practice to pass the range as a parameter.
    Remember what the dormouse said
    Feed your head

  11. #11
    Forum Contributor Mayda89's Avatar
    Join Date
    09-13-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: Problem with user defined function

    Hi romperstomper,

    Thank you for your suggestion!
    I've tried this code:
    Please Login or Register  to view this content.
    But it doesn't solve my problem.
    When I open a new workbook, and return to the original workbook, I still get #value erros on the original workbook.
    I believe I need to refer to the original workbook, but I couldn't find a way to successfully do it.


    Quote Originally Posted by romperstomper View Post
    For a UDF you should pass all ranges as arguments. If you don't, you have to make your function volatile otherwise when your referenced table expands, your functions won't recalculate automatically and properly qualify the ranges, as mentioned.
    Thanks for the explanation about why everyone insists on passing this range as an argument.
    I tried to expand my table, and checked whether the function automatically recalculates the values without me doing anything, and apparently it does automatically recalculate. So it looks like there is no need to pass this range as an argument in this case .

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem with user defined function

    Did you properly qualify all the ranges in your code?
    Please Login or Register  to view this content.
    is workbook-specific. It can only refer to the sheet codenamed ShtListe in the workbook containing the code.

    Your function should not automatically recalculate unless you also changed one or more of the input values. It seems to be pointless to continue that discussion though.

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

    Re: Problem with user defined function

    In response to PM.

    In many ways, I am inclined to agree with shg -- the easiest and, IMO, best solution is to pass the "Table4" range through the argument list (I also noticed that you also have an unqualified reference to "Table3..." down in the sumifs() statement). Converting a converting a range passed to the argument to a variant/array should be essentially the same as your Myrange=... statement is now -- but you get better definition of what the range argument refers to.

    It can be unhelpful to state that "none of these statements worked" like in post #3. Exactly what didn't work? Did you get a compiler error? a runtime error? it ran but returned the wrong value? Syntactically, many of those statements look correct to me, so it may be important to know exactly what you mean by "didn't work".

    One thing I would probably suggest: Put a breakpoint near the beginning of the function, so you can debug this from break mode where you will have access to VBA's array of debugging tools http://office.microsoft.com/en-us/su...001042819.aspx . This will allow you to explore different variables and constants using the immediate window, watch window, and locals window to see what each contains. When a variable contains something unexpected, you can investigate when and eventually why the variable is assigned an incorrect value.

  14. #14
    Forum Contributor Mayda89's Avatar
    Join Date
    09-13-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: Problem with user defined function

    MrShorty,
    Thank you very very much!
    As you have discovered, the other unqualifed ranges were also causing the problem.
    I solved the problem by qualifying all of the ranges properly, but I also had to change this code:
    Please Login or Register  to view this content.
    to this:
    Please Login or Register  to view this content.
    I thought that arrays should also be declered by using the parantheses after the array name, but I get errors if I do use the paranthesis, but the code works well if I omit the paranthesis.

    For example if I use
    Please Login or Register  to view this content.
    I get the error "type mismatch".
    If I put the array lengths in the paranthesis, and write this:
    Please Login or Register  to view this content.
    I get the error "Can't assign to array"

    Anyway, so deleting the paranthesis after the "MyRange" array and qualifying all the ranges properly solves my problem.

    So here is the final code:
    Please Login or Register  to view this content.

    I found out that after removing the paranthesis, the methods to qualify the range described in A), B), C), D), G) and H) all work!


    Again, Thank You very Much!

  15. #15
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem with user defined function

    Always specify the .Value - never leave it implicit.

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

    Re: Problem with user defined function

    I found out that after removing the paranthesis, the methods to qualify the range described in A), B), C), D), G) and H) all work!
    you have just entered that part of "The Twilight Zone" where the difference between "An array of Variants" and "A Variant containing an array" becomes important.

+ 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. User Defined Function Update problem. Strange!
    By mbga8ejf in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-24-2007, 01:46 PM
  2. problem with user defined function
    By panjo in forum Excel General
    Replies: 3
    Last Post: 06-16-2006, 06:25 AM
  3. [SOLVED] Character formating for User defined function problem
    By RosH in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-26-2005, 01:50 PM
  4. User Defined Function Problem
    By bw in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-04-2005, 10:10 AM

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