I have a spreadsheet that I implemented a timer in to automatically update a formula every 10 seconds. Everything was working great until I would open another spreadsheet. My assumption was that it was still trying to run Timer() on the wrong sheet. So, I tried switching to absolute calls. Here is my full code:
Dim TimerActive As Boolean
Sub StartTimer()
Start_Timer
End Sub
Private Sub Start_Timer()
TimerActive = True
Application.OnTime Now() + TimeValue("00:00:10"), "timetrack.xlsm!Tracker.Timer"
End Sub
Sub Stop_Timer()
TimerActive = False
End Sub
Sub Timer()
Dim tracker As Worksheet
Set tracker = Workbooks("timetrack.xlsm").Sheets("Tracker")
tracker.Range("O1").Value = "Timer Stopped"
'On Error Resume Next
If TimerActive Then
tracker.Range("O2").Value = Time
Application.OnTime Now() + TimeValue("00:00:10"), "timetrack.xlsm!Tracker.Timer"
tracker.Range("N:N").Calculate
End If
tracker.Range("O1").Value = ""
End Sub
Specifically, I'm getting an error on the Application.OnTime line:
Cannot run the macro ''S:\OneDrive...!docs\timetrack.xlsm'!Tracker.Timer'. The macro may not be available in this workbook or all macros may be disabled.
The path that shows up in the error is correct. What is wrong? What's weird is it seems to run it in the StartTimer() sub without issue.
To refer to a procedure in a worksheet, you need to use the WorkSheet.CodeName and not the WorkSheet.Name :
Application.OnTime Now() + TimeValue("00:00:10"), ThisWorkbook.Name & "!" & Me.CodeName & ".Timer"
You'll need to activate the desired workbook prior to making changes to the cells within.
Before accessing the contents within the workbook, try adding:
Workbooks("timetrack.xlsm").Activate
If the macro that runs is in "timetrack.xlsm" you could also use the following code to avoid referencing the workbook name:
ThisWorkbook.Activate
ThisWorkbook refers to the workbook in which the macro resides.
Related
I am having a little issue which is kind of strange.
So I have a macro Workbook (lets call it "Main" for simplification) with all its code that runs almost perfectly except for this little problem. "Main" is usually always open as it is our balances for the day.
When I or another user opens any other workbook at the same time as this one is open everything is fine, but when we close the workbook that doesn't have the code, "Main" runs it's before close code.
So this is my before Close code I have on "Main":
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Tsweep As Integer
Dim sTime As Date
If ThisWorkbook.Sheets("Coordenador").Range("O2") <> "" Then
For Tsweep = 2 To Range("Coordenador!O1048000").End(xlUp).Row
Application.OnTime EarliestTime:=Range("Coordenador!O" & Tsweep).Value,
Procedure:="CloseC", Schedule:=False
Next Tsweep
End If
CloseC
sTime = ThisWorkbook.Sheets("Coordenador").Range("P15") +
TimeValue("00:30:00")
Application.OnTime EarliestTime:=sTime, Procedure:="SaveWb", Schedule:=False
ThisWorkbook.Save
End Sub[/CODE]
This is the code on "CloseC":
Sub CloseC()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Coordenador")
With ws
.Unprotect Password:=pass
.Range("O:O").ClearContents
End With
For Each Worksheet In ThisWorkbook.Worksheets
Worksheet.Protect Password:=pass
Next
ThisWorkbook.Sheets("Coordenador").Visible = xlSheetVeryHidden
ThisWorkbook.Sheets("LookupList").Visible = xlSheetVeryHidden
End Sub
And the Code on SaveWb:
Sub SaveWb()
Dim vTime As Date
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Coordenador")
ThisWorkbook.Save
vTime = Now
With ws
.Unprotect Password:=pass
.Range("P15") = vTime
.Protect Password:=pass
End With
Application.OnTime vTime + TimeValue("00:30:00"), "SaveWb"
End Sub
To explain this code stops all the OnTime runs that the system might still have. It clears some cells, protects some sheets and it saves. The error than appears when the user closes "Main" and excel tries to run "SaveWb".
I was wondering if there is a way to avoid this since in theory excel runs each instance by itself and even though me and/or the users are closing these other workbooks with the close button top we are not closing "Main" and that workbook remains open. Other idea would be to check if the workbook is still open after 5 seconds and if it is to run "SaveWb" but I haven't found a way to code that.
Thank you for your help in advance!
I have found a work around for this. Thank you all for helping me reach the idea.
Basicly what I did was add a If clause at the start of the before close:
If ActiveWorkbook.Name Like "ReportingReport*" Then
(code)
Else SaveWb
End If
I have a situation where multiple users will need to access a workbook (want to avoid using the 'Share Workbook' option due to all the problems). I've determined that a possible solution to this is to get the workbook to automatically close after 15 minutes of inactivity.
I would also like a message to pop up after the 15 minutes which alerts the user that unless they click the 'okay' button, the workbook will close. If they click the button, I would like the counter to start over, and ideally if they don't click anything the workbook will automatically closer after a further 1 minute.
I have found some code online which I have used. The workbook successfully closes after a specified time but I can't figure out how to get the message box to pop up. Would appreciate any help, thanks!
Code I used is below:
In module 1:
Dim DownTime As Date
Sub SetTimer()
DownTime = Now + TimeValue("0:15:00")
Application.OnTime EarliestTime:=DownTime, _
Procedure = "ShutDown", Schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=False
End Sub
Sub ShutDown()
Application.DisplayAlerts = False
With ThisWorkbook
.Saved = True
.Close
End With
End Sub
And in ThisWorkbook:
Private Sub Workbook_Open()
Call SetTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopTimer
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call StopTimer
Call SetTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Call StopTimer
Call SetTimer
End Sub
Try the below ShutDown procedure:
Sub ShutDown()
If CreateObject("WScript.Shell").PopUp("Close Excel?", 60, "Excel", vbOKCancel + vbQuestion + vbSystemModal) = vbCancel Then
StopTimer
SetTimer
Exit Sub
End If
Application.DisplayAlerts = False
With ThisWorkbook
.Saved = True
.Close
End With
End Sub
Never 'share' Excel files on a network drive with coworkers. You will encounter all kinds of problems, including workbook corruption, and other things. Try this script, to auto-close your Excel files after n-minutes of inactivity.
To start, add the following code to a standard macro module. Note that there are three routines to be added:
Dim DownTime As Date
Sub SetTimer()
DownTime = Now + TimeValue("01:00:00")
Application.OnTime EarliestTime:=DownTime, _
Procedure = "ShutDown", Schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=False
End Sub
Sub ShutDown()
Application.DisplayAlerts = False
With ThisWorkbook
.Saved = True
.Close
End With
End Sub
The next routines (there are four of them) need to be added to the ThisWorkbook object. Open the VBA Editor and double-click on the ThisWorkbook object in the Project Explorer. In the code window that Excel opens, place these routines:
Private Sub Workbook_Open()
Call SetTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopTimer
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call StopTimer
Call SetTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Call StopTimer
Call SetTimer
End Sub
See this for all info.
http://excelribbon.tips.net/T008192_Forcing_a_Workbook_to_Close_after_Inactivity.html
I'm running a macro in one workbook which is constantly running. When I open another workbook the macro breaks and its falls out of range. How can I lock the macro on that particular sheet? macro code is:
Sub startTimer()
On Error Resume Next
ActiveSheet.Unprotect Password:="***"
Application.OnTime Now + TimeValue("00:00:01"), "Increment_count", Schedule:=False
Application.OnTime Now + TimeValue("00:00:01"), "Increment_count"
End Sub
Sub Increment_count()
If ActiveCell.Column = 9 Then
ActiveCell.Value = ActiveCell + 1
startTimer
Else
MsgBox "Timer works only in I column", vbCritical, "Error"
End If
End Sub
Sub stopTimer()
On Error Resume Next
Application.OnTime Now + TimeValue("00:00:01"), "Increment_count", Schedule:=False
ActiveSheet.Protect Password:="***"
ActiveWorkbook.Save
End Sub
This answer is an extension of the comment by Shai Rado
Activeworkbook and Activesheet reference to the sheet currently focussed on. A better way is to name your worksheet of interest and get that worksheet by its name.
Dim correctSheet as Worksheet
correctSheet = Worksheets("SheetName")
correctSheet.Unprotect ' etc..
Look also to this question:Declaring variable workbook / Worksheet vba
This reference might also be useful
http://www.excel-easy.com/vba/workbook-worksheet-object.html
I've been struggling with this for a few days.
I have the below code which copies and pastes value when a certain If condition is met (the workbook has live data feeding into multiple sheets every second).
The code is currently located in a module, as the IF condition can apply to all sheets in the workbook.
The problem I have is the code only runs on the activesheet. I need it to run on all worksheets in the workbook. I've tried multiple loops with no success. Ideally I need the code to run across all sheets in the background (i.e. without activating them). Any help will be appreciated.
Dim TimeToRun
Sub auto_open()
Call SchedulePrices
End Sub
Sub SchedulePrices()
TimeToRun = Now + TimeValue("00:00:15")
Application.OnTime TimeToRun, "CopyPrice"
End Sub
Sub CopyPrice()
Calculate
If Range("AM7") = "1" Then
Range("AM10:AM69").Value = Range("K9:K68").Value
Range("AL10:AL69").Value = Range("B9:AM68").Value
Range("AM8:AM9").Value = Range("C2:C3").Value
End If
'run the timer sub
Call SchedulePrices
End Sub
Sub auto_close()
On Error Resume Next
Application.OnTime TimeToRun, "CopyPrice", , False
End Sub
Sub CopyPrice()
Dim sht as Worksheet
For Each sht in Worksheets
If sht.Range("AM7") = "1" Then
sht.Calculate
sht.Range("AM10:AM69").Value = sht.Range("K9:K68").Value
sht.Range("AL10:AL69").Value = sht.Range("B9:AM68").Value
sht.Range("AM8:AM9").Value = sht.Range("C2:C3").Value
End If
Next
'run the timer sub
Call SchedulePrices
End Sub
I have come across a strange glitch in a code that I found online. if I have two different spreadsheets open, one with this code in it and another that may well have no macros at all, and i leave that one alone for the specified time (as is the time I allow for in the macro) then the spreadsheet that has the macro in closes on me and then after the given time, the other spreadsheet closes too. Why does this happen?
Module 1:
Dim DownTime As Date
Sub SetTimer()
DownTime = Now + TimeValue("00:00:20")
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=False
End Sub
Sub ShutDown()
Application.DisplayAlerts = False
Call ExampleToSaveWorkbook
With ThisWorkbook
.Saved = True
.Close
End With
End Sub
Sub ExampleToSaveWorkbook()
'Saving the Workbook
bnam = ActiveWorkbook.Name
filenm = "S:\Economics\GTAA\dailyPM\excelfiles\backups\" & bnam
ActiveWorkbook.SaveAs filenm
End Sub
ThisWorkbook
Private Sub Workbook_Open()
Call SetTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopTimer
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call StopTimer
Call SetTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Call StopTimer
Call SetTimer
End Sub
Why is it closing more than the 1 workbook which has the macro?
The Shutdown sub calls ExampleToSaveWorkbook which closes the active workbook.
The remainder of the Shutdown then proceeds to close the ThisWorkBook object.