excel+training - vb times
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel Training and help » excel+training - VB Times

excel+training - VB Times

resolvedResolved · Low Priority · Version Standard

replyReply Fri 16 Nov 2007, 16:37Delegate Suzie said...

Suzie has attended:
Excel VBA Intro Intermediate course

VB Times

How can you get Excel to check the time for when an hour has passed?

For upcoming training course dates see: Pricing & availability

replyReply Wed 21 Nov 2007, 17:13Trainer Carlos said...

RE: VB Times

Suzie

The code below records the time the Workbook was opened and will inform you when an Hour has passed.

This code needs to be placed in the This Workbook code area of the VBE. (In the Project window - Double click This Workbook to open it)

Paste it immediately below the Option Explicit


Dim StartTime As Date
Dim NewTime As Double

Private Sub Workbook_Activate()

StartTime = Now()

End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

NewTime = Now() - StartTime

If NewTime >= 0.041666667 Then 'This is the numeric value for 1 hour

MsgBox "An hour has passed"

End If

End Sub


Hope this helps.

Carlos

replyReply Fri 23 Nov 2007, 14:12 Edited on Fri 23 Nov 2007, 14:13Trainer Carlos said...

Inform user One Hour has passed

Hi Suzie

Further to the code I gave you earlier the code below is simpler and will give you a message every hour. Note that :

1. This code needs to be placed in the ThisWorkbook code area of the VBE. (In the Project window - Double click ThisWorkbook to open it)

2. Paste the code immediately below the Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)

'This switches off the OnTime function otherwise the workbook would reopen every hour to run the macro

Application.OnTime RunMacroTime, "MyMacro", , False

End Sub

Private Sub Workbook_Open()

Application.OnTime Now + TimeValue("00:60:00), "MyMacro"

End Sub

In the Module your Macro is in you need to make the following changes:

Public RunMacroTime As Date
'This is the variable that holds the time so the procedure can be cancelled on Close

Sub MyMacro()

RunMacroTime = Now + TimeValue("00:60:00") 'Resets the timer to run in 60 mins
Application.OnTime RunMacroTime, "MyMacro" 'Reruns the macro

MsgBox "1 Hour Has Passed"

End Sub


Regards

Carlos

 

Excel tip:

Change the Value of a Constant

When using a named constant in a worksheet, you may wish to change the value of that constant.

From the 'Insert' menu, select 'Name', then select 'Define'.

In the 'Define Name' dialog box, select the constant that you want to change.
Change the value in the 'Refers To' box.
Click OK.

Wherever that named constant has been used it will now use its new value.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard