Application.SendKeys "({1068})" doesnt work together with Application.OnTime - excel

I'm trying to get a macro on a schedule through Application.OnTime as below (located in ThisWorkbook):
Private Sub Workbook_Open()
Application.OnTime TimeValue("15:50:02"), "CopyScreen"
End Sub
Whereas the "CopyScreen" sub is as below (located in regular module):
Sub CopyScreen()
Application.SendKeys "({1068})", True
DoEvents
ActiveSheet.Paste
End Sub
The CopyScreen() module works fine when calling it from a manual prompt (F5), but whenever calling it through the scheduled Workbook_Open() sub, a black screenshot gets copied. Any ideas here?

I got it to work with this code:
Sub CopyScreen()
Dim sht As Worksheet
Set sht = ActiveSheet
Application.SendKeys "({1068})", True
DoEvents
With sht
.Activate
.Paste
End With 'sht
Apparently, Excel forgets ActiveSheet when it goes to sleep waiting for the set time. Go figure!
HTH
End Sub

Related

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")

Getting error for macro when moving to a different sheet

Im getting a Run-time error '1004':
Select method of range class failed
This only happens when I move away from the sheet (Bet Angel) that has the macro running to another sheet. The code is below:
Sub DeleteStatus()
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
I want the macro to run even if I am moving between different sheets in the workbook.
Since you are moving to another worksheets, you need to activate or select the target worksheet first.
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
If you want to go back to your original worksheet, you can activate it at the end of DeleteStatus()
Like this
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
Sheets("your original worksheet").Activate
End Sub
If you don't want to use activate, then you need to dim the worksheet first.
Like this
Sub DeleteStatus()
Dim ws As Worksheet
Set ws = Sheets("Bet Angel")
ws.Range("O6:O50").ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub

How to Fire OnTime Event by a Workbook.Close Statement?

It appears that if the OnTime event is registered by a programmatic MyBook.Close statement, then OnTime never runs.
This code works fine:
Sub TestOnTime()
Application.OnTime Now + TimeValue("00:00:05"), "MySub"
End Sub
Sub MySub()
Debug.Print "hello"
End Sub
Run TestOnTime. MySub will execute, as expected.
And this code runs fine:
ThisWorkbook:
Dim WithEvents oApp As Application
Private Sub Workbook_Open()
Set oApp = Application
End Sub
Private Sub oApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
Application.OnTime Now + TimeValue("00:00:05"), "MySub"
End Sub
Module 1:
Sub MySub()
Debug.Print "hello"
End Sub
Manually close another workbook to fire oApp_WorkbookBeforeClose.
MySub executes, as expected.
But this code fails. The OnTime event never runs.
Book 1
ThisWorkbook:
Dim WithEvents oApp As Application
Private Sub Workbook_Open()
Set oApp = Application
End Sub
Private Sub oApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
Application.OnTime Now + TimeValue("00:00:05"), "MySub"
End Sub
Module 1:
Sub MySub()
Debug.Print "hello"
End Sub
Book 2
Module 1:
Sub Test()
ThisWorkbook.Close
End Sub
Run Test to close Book 2.
Book 1 oApp_WorkbookBeforeClose
executes, as expected.
But the Book 1 MySub event never runs.
Why?
Why doesn't OnTime execute if registered by a Workbook_BeforeClose event? No code is running in the book that's closing. OnTime works no problem with other events (eg programmatically opening a workbook). Somehow, closing a workbook programmatically breaks OnTime. Why?
As Book 2 is being closed, You should include the Application.OnTime procedure in Book 2 and not in Book 1
Also, I think those books should be saved once and not new books.
Sub test()
Application.OnTime Now + TimeValue("00:00:05"), "Book 1.xlsm!MySub"
ThisWorkbook.Close
End Sub
EDIT Jul 6 -
You are closing the workbook and then you are trying to run a macro MySub in the same workbook after 5 seconds. Macro in the same workbook will not run once the book is closed. Application will reopen the file to run the macro. If you want to close Book2 after 5 seconds of closing Thisworkbook then --
in Thisworkbook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime Now + TimeValue("00:00:05"), "Book2.xlsm!Test"
End Sub
So, after closing Thisworkbook, macro named "Test" in Book2 will run and will close that workbook.

Clearing Excel AutoFilter using VBA

I am using this VBA code to refresh my entire workbook at specific time intervals. (thanks to this thread)
As you can see, it is currently set to refresh every 60 minutes.
Public RunWhen As Double
Public Const cRunIntervalMinutes = 60
Public Const cRunWhat = "Workbook_RefreshAll"
Sub StartTimer()
RunWhen = Now + TimeSerial(0, cRunIntervalMinutes, 0)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliesttime:=RunWhen, _
procedure:=cRunWhat, schedule:=False
End Sub
Sub Workbook_RefreshAll()
Application.CalculateFullRebuild
ActiveWorkbook.RefreshAll
Call StartTimer
End Sub
The code works great, however there’s one thing I wish it could also do:
Any time the sheet is opened, and/or
Any time this VBA code executes the sheet refresh process
I would like the AutoFilter to be "Cleared" (not disabled or turned off) as in the screen grab below:
AutoFilter Clear
Thanks in advance for any assistance.
Cheers
This should do the trick. You may of course do it for one Sheet only, if you wish.
Sub Workbook_RefreshAll()
Dim sh As Worksheet
Application.CalculateFullRebuild
ActiveWorkbook.RefreshAll
For Each sh In ActiveWorkbook.Worksheets
If Not (sh.AutoFilter Is Nothing) Then sh.AutoFilter.ShowAllData
Next sh
Call StartTimer
End Sub

Refresh data and exit with saving Macro 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

Resources