+ Reply to Thread
Results 1 to 9 of 9

Simple polynomial produces error ONLY for a certain input, otherwise works

  1. #1
    Registered User
    Join Date
    04-14-2014
    Location
    Munich, Germany
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    4

    Question Simple polynomial produces error ONLY for a certain input, otherwise works

    Dear All,

    I'm new here, and new to Visual Basic, (but having a background in C and C++).
    Sorry if my question is foolish, but I'm really puzzled by the problem I'm facing now.

    I'm trying to write a simple user defined function, "calctemp(sensor, resistance As Double) As Double" below, which calculates a polynomial to the 6th degree
    It should take the 7 coefficients of this polynomial (K_1 ... K_7) from cells in the same row as input cell "sensor" (with offset +1, +2... +7 to the right from that input cell).
    And "resistance" is the variable taken with double precision, as the second input.

    I wrote the code below. And it worked nicely, with the desired precision.
    But, if I input resistance = 2774.0
    it gives #VALUE! error. Regardless from the polynomial coefficients! Regardless from anything else, seemingly, except for the power of the polynomial.
    This error occurs at power 5, and present above.
    I don't know what's so special about this number 2774.0, already 2774.000001 gives a numerically correct result. Other round numbers (3300.0, whatever) work too, and I did not find other input values failing so far.

    Am I doing something incorrect in the code? I'm not sure my usage of "sensor.Offset(0, 1).Value" is the recommended way to select cell values next to the input cell (in principle, I would think the input "sensor" would need a type, like "As Address", to be sure we are referring to the cell address, and shifting that with the .Offset(,) method) But again, this part seems to work...
    I would be glad for any debugging ideas!
    I'm using Excel for Mac 2011.

    PHP Code: 

    Function calctemp(sensorresistance As Double) As Double

        Dim res 
    As Double
        
        Dim K_1 
    As DoubleK_2 As DoubleK_3 As DoubleK_4 As Double
        Dim K_5 
    As DoubleK_6 As DoubleK_7 As Double
        
        res 
    1000 resistance
        K_1 
    sensor.Offset(01).Value
        K_2 
    sensor.Offset(02).Value
        K_3 
    sensor.Offset(03).Value
        K_4 
    sensor.Offset(04).Value
        K_5 
    sensor.Offset(05).Value
        K_6 
    sensor.Offset(06).Value
        K_7 
    sensor.Offset(07).Value
        
        calctemp 
    K_1 K_2 res K_3 res K_4 res K_5 res K_6 res K_7 res 6
            
    End 
    Function 

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

    Re: Simple polynomial produces error ONLY for a certain input, otherwise works

    I don't see anything syntactically wrong with the code. Have you tried stepping through the function one step at a time? http://office.microsoft.com/en-us/su...819.aspx?CTT=1 By stepping through, hopefully you can identify the statement that is causing the error and note the values of the different variables and identify the source of the error.

    Will it calculate correctly in the spreadsheet? The final function is relatively simple (a single =SERIESSUM() function should do it). I don't know how diagnostic it will be, but it might be interesting to know if Excel's native function has the same problem with the same input or not. (It also might suggest an alternative to using the VBA UDF, and we can sidestep the problem completely).
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    04-14-2014
    Location
    Munich, Germany
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    4

    Re: Simple polynomial produces error ONLY for a certain input, otherwise works

    Thank you very much for your reply! As for the debugging, I can insert breakpoints, but Debug -> Step Into just simply has no effect at all. Could not figure out why. (Excel for Mac 2011 14.0.0) I start to think something is broken (with me, or with my excel )
    I send an example spreadsheet in attachment, if you have some time & interest to look at the problem.

    There is nothing wrong with mathematical formula, the same expression calculates the numerical values nicely. I plot this in row J in the example,where I don't use predefined functions, just basic expressions directly in the spreadsheet and usual references to the cells.

    Next to it in row K you can see the output of the function calctemp(sensor,resistance) (code in Module1), which is supposed to operate with the same input values.
    Indeed, for most of the "resistance" inputs (row I in example) it gives the very same result as the direct expression. But the function fails if resistance = 2774.0 (regardless from the "sensor" input).

    I'm quite confused...
    Attached Files Attached Files

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

    Re: Simple polynomial produces error ONLY for a certain input, otherwise works

    I cannot seem to replicate the problem. The value errors are present when I open your file, but I enter the error cell, press F2 and enter (to recalculate that cell, or do something else to force it to calculate the function in the error cell), it displays the correct answer, and I cannot recreate the error value. It would appear to me that the error is something more sinister in your spreadsheet/installation and not a problem with the code.

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Simple polynomial produces error ONLY for a certain input, otherwise works

    in principle, I would think the input "sensor" would need a type, like "As Address", to be sure we are referring to the cell address, and shifting that with the .Offset(,) method
    To this point, I think you're looking for Range, if you really want to give a datatype to the variable

    Please Login or Register  to view this content.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  6. #6
    Registered User
    Join Date
    04-14-2014
    Location
    Munich, Germany
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    4

    Re: Simple polynomial produces error ONLY for a certain input, otherwise works

    Thanks a lot for trying it, MrShorty! Yes, I got this feeling that something is badly broken when the debugging tools did not work -- I just did not have another machine to try it.
    Uh-huh. Well, I guess I check whether my institute has some upgrades, or I try to reinstall this one.
    Interesting, I've never seen such a glitch before. Very Scary.

    And thanks for the tip, Solus Rankin!

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

    Re: Simple polynomial produces error ONLY for a certain input, otherwise works

    I find that "debug->step into" never works right for UDF's like this. This command just does not have any way of specifying the arguments (sensor and resistance in your case) for the function call when called from "debug -> step into". With UDF's like this, I find that the best way to get into "debug" mode is to set a break point somewhere in the function (often I will set the breakpoint on the function statement itself), then call the function from a spreadsheet cell.

  8. #8
    Registered User
    Join Date
    04-14-2014
    Location
    Munich, Germany
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    4

    Re: Simple polynomial produces error ONLY for a certain input, otherwise works

    Thanks for the tips! I upgraded: Excel for Mac, 14.3.0, and the glitch disappeared. However it makes me a bit nervous, I hope there aren't other (silent) numerical errors from broken VBA libraries or whatever. This is scary stuff. Is Mac normally an OK-platform for programming Excel? ... I got so excited, I was about transfer part of my data analysis to Excel when I realized yesterday you can program it 'easily', but this discouraged me a little.

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

    Re: Simple polynomial produces error ONLY for a certain input, otherwise works

    I am not at all familiar with Excel for Mac -- I work exclusively with Windows machines. I have not had any similar problems with my UDF's. In general, I would say that Excel/VBA has been a fairly stable, reliable platform for programming many different types of data analysis. I can't explain this little hiccup you've experienced, but I would guess that it is a rather rare occurrence.

+ 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] Simple Macro Works for Me but 1004 Error for User
    By snapfade in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-07-2013, 01:44 PM
  2. [SOLVED] Excel produces Error when the code works
    By WadeLair in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2012, 11:15 PM
  3. Replies: 2
    Last Post: 12-18-2008, 11:23 AM
  4. [SOLVED] Input box cancel produces error
    By pkeegs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-12-2006, 01:10 AM
  5. simple copy column works but ERROR!
    By Jack in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-30-2005, 02:00 PM

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