CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2003
    Posts
    322

    Simple timing measurements in VBA

    I had used VB6 up until ~2 years ago, so I've forgotten some things.

    I'm now maintaining some Excel VBA macros which is reprocessing a text file, then displays stuff about the file in a spreadhseet

    I'd like to measure the time it takes to process the file, but can't seem to find the right functions to do it. I only need 1-second resolution.

    I've looked through VBA in a nutshell, but the time-related calls dont seem to do what I need (or I'm not looking in the right place.

    StartTime = SomeTimeFunction()

    ' code to process the file

    StopTime = SomeTimeFunction()

    Elapsed = (StopTime-StartTime)


    I've though about adding a timer using 1-second callbacks, to increment a count, but I can't seem to add any VB controls to the Control Pallete in The VBA Editor in Excel 2003.

    (VB6 let me add tons of controls.)

    Does anyone have any code I can look at to get the elapsed time in seconds?

    thanks

  2. #2
    Join Date
    May 2001
    Location
    Silicon Valley
    Posts
    113

    Re: Simple timing measurements in VBA

    Quote Originally Posted by cappy2112
    ...I only need 1-second resolution.
    Lookup the GetTickCount function. This Windows API function returns a number of millisecond that had elapsed since since windows started.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Simple timing measurements in VBA

    Excel had Time/Date fields, so you can use DateDiff() and DateAdd() to get time in hours, minutes, seconds, days, weeks, etc.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    Re: Simple timing measurements in VBA

    Use this:

    Code:
    StartTime = Timer
    
    'Code to process the file
    
    Elapsed = (Timer - StartTime)

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Simple timing measurements in VBA

    GetTickCount is actually what you need. There are numerous examples here on CG and even on the other sites.

  6. #6
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    Re: Simple timing measurements in VBA

    and what about 'Timer' function?

  7. #7
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054

    Re: Simple timing measurements in VBA

    Normally I am using a named range plus this code
    Code:
    Public Sub perform_task()
        Range("runtime_start").Value = Now()
        task1
        task2
        task3
        Range("runtime_end").Value = Now()
    End Sub
    Then a third cell returns Range("runtime_end").Value minus Range("runtime_start").Value
    Marketing our skills - please participate in the survey and share your insights
    -

  8. #8
    Join Date
    Apr 2003
    Posts
    322

    Re: Simple timing measurements in VBA

    Quote Originally Posted by aio
    Normally I am using a named range plus this code
    Code:
    Public Sub perform_task()
        Range("runtime_start").Value = Now()
        task1
        task2
        task3
        Range("runtime_end").Value = Now()
    End Sub
    Then a third cell returns Range("runtime_end").Value minus Range("runtime_start").Value

    Thanks to everyone who replied.
    It's starting to come back to me now

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured