Refresh data and exit with saving Macro Excel - excel

I need to refresh the data from ole db source while opens excel and afte that saves and exit.
Here is my macro code:
Sub auto_open()
Call DataRefresh
End Sub
Sub DataRefresh()
TimeToRun = Now
Application.OnTime TimeToRun, "Refresh"
End Sub
Sub Refresh()
ActiveWorkbook.Connections("Shas").Refresh
End Sub
Sub auto_close()
Application.OnTime TimeToRun, "Refresh", , True
Application.Quit
ThisWoorkbook.Close SaveChanges:=True
End Sub
It's okay with renewing after openening but it doesnt exit. What am I doing wrong?

You don't actually tell the workbook to close at any point...
Also, the Auto_Open() routine is technically triggered after the workbook has finished opening so you don't need to call separate routines:
Also Also - You can't close a workbook after you've quit the application...
Try just this instead:
Sub auto_open()
ActiveWorkbook.Connections("Shas").Refresh
DoEvents
ThisWoorkbook.Close SaveChanges:=True
End Sub

Related

Excel Application.ontime macro re-opens workbook

I have a workbook that is using some macros to automatically close it after some idle time elapses.
There is 1 issue, if the user has other excel workbooks (regardless if its xlsx or xlsm) after the procedure for it to close the target file. it REOPENS the target file again and can continue in a loop like this until the user closes all of their open workbooks quitting the application.
any advice on how to tweak this code so that the Application.OnTime events will stop effectively when the target workbook has closed would be appreciated.
The goal was simple:
If a user leaves their desk, or they are working on another workbook and they forget to close the file it will save and close the workbook and only that workbook without disrupting any other function or workbook
this does work fine on its own only if and only if the workbook that has this code is the only book open in the application.
In "Thisworkbook" I have below
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopTimer
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call StopTimer
Call SetTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Call StopTimer
Call SetTimer
End Sub
In a separate module I have below:
Dim DownTime As Date
Sub SetTimer()
DownTime = Now + TimeValue("00: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 Workbooks("Customer Complaint Tracker.xlsm")
.Saved = True
.Close
End With
End Sub
I have tried using a different method to run a timer which I think worked on windows timing instead of the application itself but I could not get it to function.

Workbook.close Cannot run the marco "https://wfp-mysharepoint.com............"

Background infromation:
This workbook is on shared point and I'm trying to run a Marco in Desktop app.
The objective on the marco is to close the workbook automatically after 10 mins. I want to ensure that users do not keep the workbook open without using it.
However, when I run the below code, I get an error message
"Cannot run the marco "https://wfp-mysharepoint.com............" This Macro may not be available in this workbook or marcos maybe disabled.
The code is as follows:
Private Sub Workbook_Open()
Picktime
End Sub
Sub Picktime()
savetime = Hour(Now) & ":" & Minute(Now) + 1 & ":" & Second(Now)
Application.OnTime savetime, "Please_close"
End Sub
Sub Please_close()
ThisWorkbook.Close (True)
End Sub
The procedure Workbook_Open is an event (that runs automatically) on opening of the workbook and therefore has to be in the scope of the workbook ThisWorkbook. Other events that are worksheet related are located in the scope of their worksheet.
All other code should go into a normal module. Especially code that needs to be found by Application.OnTime can only be located in a normal module.
So in ThisWorkbook:
Private Sub Workbook_Open()
Picktime
End Sub
In a normal module:
Public Sub Picktime()
Dim SaveTime As Variant
SaveTime = Now() + TimeValue("00:01:00")
Application.OnTime EarliestTime:=SaveTime, Procedure:="Please_close"
End Sub
Public Sub Please_close()
ThisWorkbook.Close SaveChanges:=True
End Sub
Note that it is easer to add one minute to Now() by using Now() + TimeValue("00:01:00")

Excel Function 'Application.OnTime' still running after closing the workbook

I am using Excel (Office 365).
here is a video with my problem: link
The internet is full of examples where people say that the code should work when we set Schedule to False but sadly that did not help me.
The code should start a cycle using Application.OnTime and stop it before closing the workbook.
If I run the function stopF manually, in the VBA Editor, it stops Application.OnTime properly.
But if I close the workbook, it still opens up and continues executing Application.OnTime.
' In Module1:
Public startingTime As Double
Sub startF()
MsgBox "hello"
startingTime = Now + TimeValue("00:00:10")
Application.OnTime startingTime, "Module1.startF"
End Sub
Sub stopF() 'HERE IT WORKS GOOD
On Error Resume Next
Application.OnTime startingTime, "Module1.startF", , False
End Sub
' In ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call stopF 'HERE IT DOES NOT WORK GOOD
End Sub
I don't have a clue what could I do more.
My temporary solution to my question:
I left the whole code like it was and added into Module1 following code:
' In Module1:
Sub closeWorkbook()
Call stopF
ThisWorkbook.Close
End Sub
The point is:
for manual closing stopF called from Workbook_BeforeClose will work
for vba closing stopF called from a module will work.
A bit more detailed:
when a user closes a workbook manually (closes the Excel window) then Workbook_BeforeClose => Call stopF will be triggered and it will stop Application.OnTime properly.
if I want to close the workbook via code then I do not rely on the Workbook_BeforeClose function (because it doesn't work for this somehow) but I call my new closeWorkbook() function when needed, which does terminate Application.OnTime and then closes the workbook.

Auto-save in VBA is reopening closed files

I implemented some code into a few of my excel sheets that would cause the file to autosave periodically. The issue I am running into with the code, is that when it is executed, it reopens the files that have been closed that also contain the same code.
I am looking for a way to have VBA autosave documents every so often, but it would no longer run if the file isn't open.
This is the code I have implemented:
Contained in "ThisWorkbook":
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("03:00:00"), "Save1"
End Sub
Contained in "Module 3":
Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("03:00:00"), "Save1"
End Sub
A note: The code between all of the documents is 100% identical (except the TimeValue, which varies by a few hours among some of them).
Try the next approach, please:
Adapt the OnTime call to a better qualified Sub:
Private Sub Workbook_Open()
scheduleTime = Now + TimeValue("03:00:00")
Application.OnTime scheduleTime, "Module3.Save1"
End Sub
Make the Sub in discussion Private, and create a new Public variable on top of the module:
Public scheduleTime As Date
Private Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
scheduleTime = Now + TimeValue("03:00:00")
Application.OnTime scheduleTime, "Module3.Save1"
End Sub
Clear the already set OnTime procedure:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime scheduleTime , "Module3.Save1", , False
End Sub
The first two items will solve the opening of other workbooks when the active workbook procedure is called. But, this should not happen and I am afraid that this is not the real opening mechanism, the workbooks are open by their own OnTime function call.
If my above supposition is True, the item 3 alone will solve the issue...
As you may notice, OnTime is an Application method, not a Workbook method.
This means that, even if you close the Workbook, so long as the Excel Application is still open that OnTime is still counting down. Once it hits zero, it will try to run the Subroutine - but the workbook is closed. Rather than throw an Error, Excel will reopen the Workbook, and then run the Subroutine.
In order to stop this, you will need to unscheduled the Subroutine before you close the Workbook, which will also mean Storing the time. So, you will need a Module that looks something like this:
Option Private Module
Option Explicit
Private AutoRunTime AS Date
Public Sub AutoSaveWorkbook()
ThisWorkbook.Save
AutoRunTime = Now()+TimeSerial(3,0,0) '3 hours
Application.OnTime AutoRunTime, "AutoSaveWorkbook", True
End Sub
Public Sub CancelAutoSave()
Application.OnTime AutoRunTime, "AutoSaveWorkbook", False
End Sub
Then, in the Workbook_Open even you call AutoSaveWorkbook to start things off, and in the Workbook_BeforeClose even you call CancelAutoSave to end them

Proper VBA code to close workbook without saving

Whenever the code is executed while multiple workbooks are open, office stops working with a message
Microsoft Office Excel has stopped working
Windows can try to recover your information and restart the program.
What's wrong with the code? I'm using MS Office 2007 on Windows7
Private Sub Workbook_BeforeClose(Cancel As Boolean)
close_without_saving
End Sub
Sub close_without_saving()
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
If Application.Workbooks.Count < 2 Then
Application.Quit
Else
ThisWorkbook.Close
End If
End Sub
If you want to close the workbook without incorporating changes. Then you can use code like this in workbook module ~
Sub Auto_Close()
ThisWorkbook.Saved = True
End Sub
You can also use this for closing workbook without saving changes.
Sub CloseBook2()
ActiveWorkbook.Close savechanges:=False
End Sub
This routine can be attached to Close X Button. Workbook never closes partially, it will always close with all sheets contained in this workbook. DisplayAlerts = False and subsequently True can be incorporated in the routine. That should not create a problem like
Sub CloseBook()
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub

Resources