Consulting

Results 1 to 13 of 13

Thread: Solved: Timer

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Solved: Timer

    In another question, I added a timer to some simple code, but in some runs it is giving me negative values, eg -4.65E-12. Any thoughts as to why this would happen?

    [VBA] Sub Colours()
    Dim cel As Range
    Dim tim As Double
    tim = Timer
    For Each cel In Intersect(Range("A:O"), ActiveSheet.UsedRange)
    Select Case cel
    Case Is = 1
    cel.EntireRow.Interior.ColorIndex = 6
    End Select
    Next
    [a1] = Timer - tim
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Malcolm,

    I thought Timer would be a Date, not Double?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Ken,
    It appears to be Single

    Timer Function



    Returns a Single representing the number of seconds elapsed since midnight.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Well go figure that. All my examples I have on file are variants, so I just guessed.

    And so... did you run your test over midnight? VBAX says its 11:30PM your time...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Okay, I made up this example, which shows the issue pretty well:

    [vba]Sub TimeTest()
    Dim x As Single
    Dim FlashStart As Single
    Dim SubStart As Single
    SubStart = Timer
    For x = 1 To 1000
    FlashStart = Timer
    Cells(x, 1).Value = Timer - FlashStart
    Next
    Cells(1001, 1).Value = Timer - SubStart
    MsgBox "1000 iterations took " & Cells(1001, 1).Value & " seconds to complete."
    End Sub[/vba]

    I don't really get it either. Even if the numbers were being rounded due to insufficient decimal places, it a number should never be less than it's predecessor. Very strange indeed...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Strange indeed. I didn't get any negative results with Malc's code but all negative with Ken's
    Floating point maths and 21st century PCs - distant cousins at times
    M$ have lots of excuses here and, perhaps surprisingly a solution:

    In some cases, you may be able to prevent rounding errors from affecting your work by using the Precision as displayed option. This option forces the value of each number in the worksheet to be the displayed value. To turn on this option, click Options on the Tools menu. On the Calculation tab, click to select the Precision as displayed check box.
    K :-)

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by mdmackillop
    In another question, I added a timer to some simple code, but in some runs it is giving me negative values, eg -4.65E-12. Any thoughts as to why this would happen?

    [VBA] Sub Colours()
    Dim cel As Range
    Dim tim As Double
    tim = Timer
    For Each cel In Intersect(Range("A:O"), ActiveSheet.UsedRange)
    Select Case cel
    Case Is = 1
    cel.EntireRow.Interior.ColorIndex = 6
    End Select
    Next
    [a1] = Timer - tim
    End Sub
    [/VBA]
    Hi Malcolm,

    Perhaps your PC's running so fast it's exceeded the speed of light and travelled backwards in time, consequently the procedure is ending at a time before it started.... Keep your fingers clear of the keyboard so you don't get dragged into the time vortex.

    Seriously though, I ran this and only got positive numbers - perhaps the format of cell A1?

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Ahem...

    In some cases, you may be able to prevent rounding errors from affecting your work by using the Precision as displayed option. This option forces the value of each number in the worksheet to be the displayed value. To turn on this option, click Options on the Tools menu. On the Calculation tab, click to select the Precision as displayed check box.
    BS! Give it a shot...

    It didn't seem to have any effect on my results at all. In addition, my calculation, even after setting it back, seems to be running slower. I'm hoping that I didn't mess something up by checking that box...

    If you are going to do it, though, please time it using my routine before you set the option to on, then after, then after setting it back...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  9. #9
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    In this case precision as displayed would be brutal since only the displayed decimals would be affected. Since the resulting calculation is often into the 8th decimal place...

    This is not caused at all by the floating point error. I think it is caused more by the time of day, and may need to be normalized since the "First" value that sets the base (at least around 7:30 PM when I was messing with it) was close to 70,000. but the 8th decimal place resulting math....

    I agree though...it should not result in a negative...umm...ever. Is it possible the loop occurs in the future? :0)
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Just for info, here's a fuller result.

    [VBA]Sub Colours()
    Dim cel As Range
    Dim tim As Double
    tim = Timer
    [A1] = Timer
    For Each cel In Intersect(Range("A:O"), ActiveSheet.UsedRange)
    Select Case cel
    Case Is = 1
    cel.EntireRow.Interior.ColorIndex = 6
    End Select
    Next
    [A2] = Timer - tim
    [A3] = Timer
    End Sub[/VBA]





    81576.843750000000000000000000000000
    -0.000000000004192202140984590000
    81576.843750000000000000000000000000
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    Hmm..

    [VBA]
    Sub Colours()
    Dim cel As Range
    Dim tim As Double
    tim = Timer
    [A1] = Timer
    For Each cel In Intersect(Range("A:O"), ActiveSheet.UsedRange)
    Select Case cel
    Case Is = 1
    cel.EntireRow.Interior.ColorIndex = 6
    End Select
    Next
    [A2] = Timer - tim
    [A3] = tim

    End Sub[/vba]


    produces this:

    65442.40625000000000000000 Timer
    -0.00000000000267874611 Timer -Tim in Code
    65442.40600000000000000000 Tim

    0.00024999999732244800 Timer-Tim Formula

    strange indeed. I think my bank uses variations of this math.
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I think I'll just write this off as a MS mystery. Thanks all for the contributions.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Malcolm,

    Yes, can see prob now. When there is nothing to be found I got this for one trial

    41318.691406250000000000000000000000
    -0.000000000002327027459614330000
    41318.691406250000000000000000000000

    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •