ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Automatically starting code (https://www.excelbanter.com/excel-worksheet-functions/69974-automatically-starting-code.html)

Richard

Automatically starting code
 
The code works fine, but how and where do you put it for it to start
automatically. I have it in a Module but I have to manually go in and run it
from there.
Sub clock()
If ThisWorkbook.Worksheets(1).Range("C2").Value = "X" Then Exit Sub
ThisWorkbook.Worksheets(1).Range("G2").Value = Format(Now, "hh:mm:ss AM/PM")
Application.OnTime Now + TimeSerial(0, 0, 60), "clock"
End Sub
Thanks in Advance!


JMB

Automatically starting code
 
You could put it in the ThisWorkBook module, and declare it as an event
handler, such as:

Private Sub Workbook_Open()
'your code here
End Sub

This example would run whenever the workbook is opened. If you click on the
drop down boxes in the ThisWorkBook module you'll see there are a variety of
events to which you could attach your code.

"Richard" wrote:

The code works fine, but how and where do you put it for it to start
automatically. I have it in a Module but I have to manually go in and run it
from there.
Sub clock()
If ThisWorkbook.Worksheets(1).Range("C2").Value = "X" Then Exit Sub
ThisWorkbook.Worksheets(1).Range("G2").Value = Format(Now, "hh:mm:ss AM/PM")
Application.OnTime Now + TimeSerial(0, 0, 60), "clock"
End Sub
Thanks in Advance!


Richard

Automatically starting code
 
Clock will change to current time but will not run continually if I put it in
Private Sub Workbook_Open()


"JMB" wrote:

You could put it in the ThisWorkBook module, and declare it as an event
handler, such as:

Private Sub Workbook_Open()
'your code here
End Sub

This example would run whenever the workbook is opened. If you click on the
drop down boxes in the ThisWorkBook module you'll see there are a variety of
events to which you could attach your code.

"Richard" wrote:

The code works fine, but how and where do you put it for it to start
automatically. I have it in a Module but I have to manually go in and run it
from there.
Sub clock()
If ThisWorkbook.Worksheets(1).Range("C2").Value = "X" Then Exit Sub
ThisWorkbook.Worksheets(1).Range("G2").Value = Format(Now, "hh:mm:ss AM/PM")
Application.OnTime Now + TimeSerial(0, 0, 60), "clock"
End Sub
Thanks in Advance!


JMB

Automatically starting code
 
I think all you need to do is call your sub from the event handler. It looks
like your sub uses OnTime to call itself to do the updating.

Private Sub Workbook_Open()
Call Clock
End Sub


"Richard" wrote:

Clock will change to current time but will not run continually if I put it in
Private Sub Workbook_Open()


"JMB" wrote:

You could put it in the ThisWorkBook module, and declare it as an event
handler, such as:

Private Sub Workbook_Open()
'your code here
End Sub

This example would run whenever the workbook is opened. If you click on the
drop down boxes in the ThisWorkBook module you'll see there are a variety of
events to which you could attach your code.

"Richard" wrote:

The code works fine, but how and where do you put it for it to start
automatically. I have it in a Module but I have to manually go in and run it
from there.
Sub clock()
If ThisWorkbook.Worksheets(1).Range("C2").Value = "X" Then Exit Sub
ThisWorkbook.Worksheets(1).Range("G2").Value = Format(Now, "hh:mm:ss AM/PM")
Application.OnTime Now + TimeSerial(0, 0, 60), "clock"
End Sub
Thanks in Advance!



All times are GMT +1. The time now is 12:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com