Results 1 to 8 of 8

Thread: VBA Now() In Excel

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    263
    Is there anyway to view the date and time in real time. i.e. be able to view the time down to the second?
    Thanks Alot,

    David Gottlieb
    CIW Certified Internet Webmaster
    Web Developer/Designer

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Why not just have a timer that checks every 1/2 a second, updating it with Now()?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    263
    Can you do that in VBA in Excel????
    Thanks Alot,

    David Gottlieb
    CIW Certified Internet Webmaster
    Web Developer/Designer

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Is this to add it to a cell? Use the SetTimer and KillTimer API functions, and set the callback function to update the cell.

    Open up a blank workbook. Add a new module, and enter this code:
    Code:
    Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Public Sub TimerProc(hwnd As Long, uMsg As Long, idEvent As Integer, dwTime As Long)
        Sheet1.Cells(1, 1) = Now
    End Sub
    Then, in your ThisWorkbook object, add this code:
    Code:
    Dim lTimer As Long
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        KillTimer 0, lTimer
    End Sub
    Private Sub Workbook_Open()
        lTimer = SetTimer(0, 1, 500, AddressOf TimerProc)
        Debug.Print lTimer
    End Sub
    You will see that it is the TimerProc function that makes the change, so you can add your custom action here.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    263

    Question

    Can you break that down a bit. I'm a beginner. I am not very experienced.
    Thanks Alot,

    David Gottlieb
    CIW Certified Internet Webmaster
    Web Developer/Designer

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The SetTimer and KillTimer functions allow you to stop and start timers. If you give it the handle to a window, it sends it a message, saying that the timer went off. If you don't, then you can pass a function to be called - in this case, TimerProc. The code in the Workbook simply stops and starts the timer when it opens or closes.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    263
    I can't get this to work. How do I do this...Please be patient with me. I'm not very good.
    Thanks Alot,

    David Gottlieb
    CIW Certified Internet Webmaster
    Web Developer/Designer

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    try this, it will update the time on cell "a1" every second

    Code:
    Sub startit()
    Dim MyStr As String
    Range("a1").Select
    MyStr = Format(Time, "hh:mm:ss AMPM")
    ActiveCell.Formula = MyStr
    Application.OnTime Now + TimeValue("00:00:01"), "startit"
    End Sub

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