+ Reply to Thread
Results 1 to 16 of 16

#REF! error with UDF

  1. #1
    Registered User
    Join Date
    08-22-2015
    Location
    Qatar, Doha
    MS-Off Ver
    2010
    Posts
    9

    #REF! error with UDF

    I have an issue in implementing a function inside the excel sheet. The excel sheet has 3 variables Nf, Rtype, vf. Where these values are cell inputs by user in the sheet.

    The UDF code is as follows:
    whenever I use this function to calculate inside the cell i.e. = vf4(C1,B1,D1) , The cell displays a #Ref! error. However, when I create a sub and calculate the same function, I get the correct value. Can anyone figure out what is happening inside the excel sheet?


    Please Login or Register  to view this content.
    Now the sub I use to test the function is

    Please Login or Register  to view this content.
    Last edited by freeky1; 08-23-2015 at 07:35 AM.

  2. #2
    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: Help with UDF

    Welcome to the board, freeky.

    Please take a few minutes to read the forum rules, and then edit your post to add CODE tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Help with UDF

    Hi freeky1 and welcome to ExcelForum,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)


    Since this is your first post, I will answer your question. You will get no further replies until you put CODE TAGS around your code in your original question.

    Your code works fine for me exactly as written, both in VBA and as a function in a cell. To test your function, put a breakpoint at the start of the function. After you type in the function in the cell, the breakpoint should be activated, and you can single step through the function using the f8 key.

    The following may help you now and in the future.

    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Please Login or Register  to view this content.
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    Debugger Secrets:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    Lewis

  4. #4
    Registered User
    Join Date
    08-22-2015
    Location
    Qatar, Doha
    MS-Off Ver
    2010
    Posts
    9

    Re: Help with UDF

    LJ,
    Thanks for the quick tips, I am a vba self-learner. Learning from the internet as I go. This forum helped a lot. I still get the #Ref! error. Is there a setting in excel sheet that I need to Toggle in order to match the data types which I used in the Sub test file?
    Pic1.JPGPic2.JPG
    Cheers
    Last edited by freeky1; 08-23-2015 at 04:02 AM.

  5. #5
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,885

    Re: Help with UDF

    If you post a workbook it will be much easier to help you. Replace any sensitive data with mockup data.
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

  6. #6
    Registered User
    Join Date
    08-22-2015
    Location
    Qatar, Doha
    MS-Off Ver
    2010
    Posts
    9

    Re: Help with UDF

    This is the workbook I am working on.
    Thanks
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: #REF! error with UDF

    I don't know why the #REF! is there.

    If I replace the #REF! with vf4, everything seems to work ok. It's just one of the many mysteries of Excel.

    Lewis
    Last edited by LJMetzger; 08-23-2015 at 10:59 AM.

  8. #8
    Registered User
    Join Date
    08-22-2015
    Location
    Qatar, Doha
    MS-Off Ver
    2010
    Posts
    9

    Re: #REF! error with UDF

    Darn, this value is just a step of many calculations to follow, if it is #ref! then all the functions to follow are #value!. Hopefully someone will figure this out one day.

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,420

    Re: #REF! error with UDF

    Since I tend to use R1C1 notation, what I see in r12 is =r[-8]c[560](arguments). Since VF4 is also a cell reference (in A1 notation) to the cell VF4, I think that is taking precedence over the UDF function name vf4. I would suggest renaming your function to something that Excel cannot confuse as a cell reference. Maybe Myvf4() or vf4udf() or something else that avoids the naming conflict.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  10. #10
    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: #REF! error with UDF

    A function that looks like an A1 cell reference will only work if Excel is using the R1C1 reference style.

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,420

    Re: #REF! error with UDF

    So, instead of suggesting renaming the function(s), should I encourage the OP to come join me on the dark side using R1C1 notation regularly?

  12. #12
    Registered User
    Join Date
    08-22-2015
    Location
    Qatar, Doha
    MS-Off Ver
    2010
    Posts
    9

    Re: #REF! error with UDF

    Quote Originally Posted by MrShorty View Post
    So, instead of suggesting renaming the function(s), should I encourage the OP to come join me on the dark side using R1C1 notation regularly?
    That worked perfectly,
    Great thanks

  13. #13
    Registered User
    Join Date
    08-22-2015
    Location
    Qatar, Doha
    MS-Off Ver
    2010
    Posts
    9

    Re: #REF! error with UDF

    Quote Originally Posted by MrShorty View Post
    So, instead of suggesting renaming the function(s), should I encourage the OP to come join me on the dark side using R1C1 notation regularly?
    How do you toggle the R1C1 style on, so instead of A B C I will see C1, C2 etc... ?
    Thanks

  14. #14
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,420

    Re: #REF! error with UDF

    Switching to R1C1 is done in the "Excel Options" dialog from the "office button" in the upper left. In this dialog, select "formula", then check R1C1 reference style. I am not familiar with how this option works in 2010. Back in 2002, when I first converted myself over to R1C1, these early versions of Excel seemed to be better at "remembering" that I want to use R1C1 notation. My work computer seems to remember just fine (probably because R1C1 is an option in my personal.xlsx file which opens automatically with Excel). On of my computers at home seems to never remember that I have previously selected R1C1 notation until I open a file that has previously been saved in R1C1 notation as the first file I open that session. All of that to say that there will probably be some growing pains to converting over to R1C1 notation.

  15. #15
    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: #REF! error with UDF

    Using R1C1 references is really not a good solution ...

  16. #16
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,347

    Re: #REF! error with UDF

    I agree with shg.

    I'd be using a Dynamic Named Range long before going to R1C1 type of formulas in my code. Read about DNR's at:
    http://www.bettersolutions.com/excel...G820716330.htm or http://www.ozgrid.com/Excel/DynamicRanges.htm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ 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