+ Reply to Thread
Results 1 to 14 of 14

Help with UDF #value error

  1. #1
    Registered User
    Join Date
    01-27-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    28

    Help with UDF #value error

    Hi guys,

    I've time data in text form i.e. "hh:mm:ss:fff" example: "17:12:36:250"

    Now I'm trying to write a UDF as shown below:

    Please Login or Register  to view this content.
    Now when I use the formula, I get #VALUE error. Why so?

    Any help would be highly appreciated.

    Thanks

  2. #2
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Help with UDF #value error

    When you use MID(), LEFT() and so on, you would have a string returned. So you are trying to use algebra on text. If you multiply e.g. Mid(ttext, 4, 2) by 1 like this:

    Mid(ttext, 4, 2)*1

    Then it should work.?
    Last edited by Søren Larsen; 04-16-2012 at 02:29 PM.
    Sincerely
    S?ren Larsen

    "Give a man a fish, and you'll feed him for a day. Give a man a fishing rod, and he'll steal your yacht!"

  3. #3
    Registered User
    Join Date
    01-27-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Help with UDF #value error

    ^^ thanks for the reply.

    I did not quite get you. I'm essentially trying to use algebra on returned string.

    And I know that "Mid(ttext, 4, 2)*1" will work and give me 12 as output.

    The problem I asked is , it's not working with the formula written.

    Suppose I want to extract only the "hh:mm:ss" and ignore the millisecond part, it works fine

    Please Login or Register  to view this content.
    This would return me 0.717083333, and formatting the cell to "hh:mm:ss.000" would return me "17:12:36.000"

    Now the question remains, why it's not working with the milliseconds?

  4. #4
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Help with UDF #value error

    Yes, trying to figure it out, but have not gotten there yot.

  5. #5
    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 UDF #value error

    Maybe ...

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

  6. #6
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Help with UDF #value error

    I see that we are in the good hands of shg...

    @ shg: Do you have an explanation why the above does'nt seem to work?:

    Please Login or Register  to view this content.

  7. #7
    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 UDF #value error

    Here's a clue:

    Enter

    ? 1000*3600

    in the Immediate window. What happens? How can you fix it?

  8. #8
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Help with UDF #value error

    Quote Originally Posted by shg View Post
    Here's a clue:

    Enter

    ? 1000*3600

    in the Immediate window. What happens? How can you fix it?
    Ahh, thanks a lot shg; overflow error. I found that this solved it:


    Please Login or Register  to view this content.
    Am I right in thinking that, in the original code, the calculation steps of the UDF causes 3600000 to be stored in an integer variable not able to store a constant that long, whereas if I changed the original code to this:

    Please Login or Register  to view this content.
    Then 3600000 is never stored during the calculation?
    Last edited by Søren Larsen; 04-16-2012 at 04:40 PM.

  9. #9
    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 UDF #value error

    Then 3600000 is never stored during the calculation?

  10. #10
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Help with UDF #value error

    Quote Originally Posted by shg View Post
    ...not sure what you mean, and perhaps I explained myself badly. But if you have no objections to the rest, then I'm good.?

  11. #11
    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 UDF #value error

    Numeric constants in VBA are Integer, Long, or Double, according to size. When they get involved with arithmetic among mixed types, all are converted to the size of the largest 'participant.' When you try to multiple 1000*3600, both Integer (by size) you get overflow.

    You can specify the type explicitly using type declaration characters:

    1% is an Integer
    1& is a Long
    1# is a Double

    and also ...

    1! is a Single

    Your CLng does essentially the same thing.

  12. #12
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Help with UDF #value error

    Your CLng does essentially the same thing.
    Yes, got that from the VBE Help. So to complete this thread, then using the type declaration characters, then the original could be changed to this:

    Please Login or Register  to view this content.
    Thanks for the help, and @Parv: Sorry for hijacking your thread!

  13. #13
    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 UDF #value error

    Or just change 3600 * 1000& to 3600000

  14. #14
    Registered User
    Join Date
    01-27-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Help with UDF #value error

    Quote Originally Posted by Søren Larsen View Post
    I see that we are in the good hands of shg...

    @ shg: Do you have an explanation why the above does'nt seem to work?:

    Please Login or Register  to view this content.
    Very efficient way of doing it, really great work :-)

    Quote Originally Posted by shg View Post
    Numeric constants in VBA are Integer, Long, or Double, according to size. When they get involved with arithmetic among mixed types, all are converted to the size of the largest 'participant.' When you try to multiple 1000*3600, both Integer (by size) you get overflow.

    You can specify the type explicitly using type declaration characters:

    1% is an Integer
    1& is a Long
    1# is a Double

    and also ...

    1! is a Single

    Your CLng does essentially the same thing.
    I knew that it's overflow problem, that's why I had declared the ptime as double.
    But I didn't know how to explicitly declare the data type for constants. Thank you very much.


    Quote Originally Posted by Søren Larsen View Post
    Yes, got that from the VBE Help. So to complete this thread, then using the type declaration characters, then the original could be changed to this:

    Please Login or Register  to view this content.
    Thanks for the help, and @Parv: Sorry for hijacking your thread!
    Thanks for summarising the solution. and never mind

+ 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