Time Macro

dgavin

Active Member
Joined
Feb 16, 2005
Messages
302
How do i change this so it only records the time in sheet 5 not sheet 1?





' Module level declaration of icount, inumberofcalls. This line
' must be at the top of the module sheet
Dim icount as Integer, inumberofcalls As Integer

Sub StartOnTime()
' Initialize icount to 1.
icount = 1
' Initialize inumberofcalls to 4.
inumberofcalls = 4
' Select the range of cells for formatting.
Range("A2:A" & inumberofcalls + 1).Select
' Format the selected cells as time.
Selection.NumberFormat = "h:mm:ss AM/PM"
' Start in cell A1.
Range("A1").Select
' Put the word "Time" in cell A1.
ActiveCell.Value = "Time"
' Start the OnTimeMacro.
Call OnTimeMacro
End Sub

Sub OnTimeMacro()
' Run the RunEvery5seconds macro inumberofcalls times.
If icount <= inumberofcalls Then
' Run the RunEvery5seconds macro in 5 seconds.
Application.OnTime Now + TimeValue("00:00:05"), _
"RunEvery5seconds"
' Increment icount by 1.
icount = icount + 1
Else
' Icount is greater than inumberofcalls, so exit the macro.
Exit Sub
End If
End Sub

Sub RunEvery5seconds()
' Places the current time in a cell.
ActiveCell.Offset(icount - 1, 0).Value = Format(Now(), _
"hh:mm:ss")
' Runs the OnTimeMacro again.
Call OnTimeMacro
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this:
Code:
Sub StartOnTime()
    iCount = 1
    iNumberOfCalls = 4
    
    With Worksheets("Sheet5")
        .Range("A1").Value = "Time"
        .Range("A2").Resize(iNumberOfCalls).NumberFormat = "h:mm:ss AM/PM"
    End With
    
    OnTimeMacro
End Sub
 
Upvote 0
Thanks works great.

but i have other worsheets in the workbook

a whilst the macro runs in enters the time into whatever sheet i looking at.

I need to keep it in worksheet 5
 
Upvote 0
I'm really don't know what you're doing. Maybe this will do what you want:
Code:
Sub RunEvery5seconds()
    If ActiveSheet Is Worksheets("Sheet5") Then
        ActiveCell.Offset(icount - 1, 0).Value = Now()
    End If
    OnTimeMacro
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,896
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top