How to run a sub at set intervals in Excel VBA? - excel

I have a UserForm in which I have to run a sub every 5 seconds. I know that I could use Application.Ontime Now + TimeValue("00:00:05"), "my_Procedure", but it seems to work only with macros. When I try it with a sub, I get the message:
Cannot run the macro ''xx\xx\WorkBook1.xlsm'!my_Procedure'. The macro may not be available in this workbook or all macros may be disabled.
I'm an absolute beginner in Excel and VBA. I googled a lot but all answers seem to show how to repeat a macro every x seconds. How can I repeat a sub every x seconds?

Related

How to run VBA Excel macro at a specific time and then every 2 hours thereafter

I would like a VBA Excel macro to run every morning # 8:30AM and then every 2 hours thereafter with the final one # 4:30pm (10:30am, 12:30pm, 2:30pm,4:30pm).
I have the following two scripts so far, however cannot seem to figure out how to have it start # 8:30AM:
Private Sub Workbook_Open()
Call SetTimeToRun
End Sub
Private Sub SetTimeToRun()
Application.OnTime Now() + TimeValue("02:00:00"), "LiveBook"
End Sub
If you want to run it a specific amount eg 3 times then simply do:
Application.OnTime Now() + TimeValue("02:00:00"), "LiveBook"
Application.OnTime Now() + TimeValue("04:00:00"), "LiveBook"
Application.OnTime Now() + TimeValue("06:00:00"), "LiveBook"
If you want to run it continously every 2 hours without limitation then you need to add
Call SetTimeToRun
to your procedure LiveBook. For Example
Public Sub LiveBook()
Call SetTimeToRun
'your code here …
End Sub
So everytime LiveBook runs it initiates the next run in 2 hours.
But note that the workbook needs to be open all the time in that machine otherwise the timed procedures will not run.
If you need to run a macro at a specific times like 8:30 am, 10:30 am. I recommend to use the windows scheduler to run a VB script or something that opens the workbook, runs the macro and closes the workbook. But note that no one else can use the workbook or the scheduler will fail to open the workbook (only one person can open a workbook at a time for editing).
Also see How to set recurring schedule for xlsm file using Windows Task Scheduler.

Repeat Macro For Specified Time Interval

I have four macros that I want to run every 10 minutes in a specified order. Currently I just set a reminder to run them, but would like to automate the process. Ideally I would love to have a separate macro that repeats all four macros (in order) every 10 mins.
I have tried adding Application.OnTime Now + TimeValue("00:10:00"), "Macro_Name" at the end of the four macros. Issue is two of the macros take some time to process.
Also tried creating a separate Macro "Repeat_10mins", and building it using the OnTime function. Only seems to work once, and doesn't repeat (i.e, runs 10 mins after setting but does not repeat).
Sub Repeat_10mins
Workbooks("Book1.xlsm").RefreshAll
'Repeat Macro1
Application.OnTime Now + TimeValue ("00:10:00"), "Macro1"
'Repeat Macro2
Application.OnTime Now + TimeValue ("00:10:00"), "Macro2"
(etc for Macro 3/4)
End Sub
You will need to create one more macro like this:
Sub Timer()
Application.OnTime Now + TimeValue("00:10:00"), "operation"
End Sub
Where operation is the name of the macro you want to run every 10 minutes.
On the macro you have built you will need to add only one line at the end which is Call Timer.
Explanation:
At the end of the macro operation it will call the macro timer which in 10 minutes will run operation.
Let me know if it works or if you need any more help.

Copy/Paste data to other existing workbook on timer

I currently have this VBA -
Sub StartTimer()
Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"
End Sub
Sub AutoCalc()
Application.CalculateFull
Sheets("Kine").Range("B603:E603").Copy _
Destination:=Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1")
Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"
End Sub
The .OnTime command is working perfectly without the Copy/Paste section, which is great.
This gives me a list of values from an SQL query that will auto-update, as well as an average at the bottom of each column of values.
I'm trying to set this up so that the average will automatically be added onto columns in Minitab but I believe that the Macro is stopping the Auto-Update in Minitab.
So my idea is to copy-paste the averages into an Excel Workbook that has no macros of its own and then link that to Minitab.
Have I put in the Copy-Paste code wrong or is there some issue with where the macros need to be stored and how?
Quick Edit - I should add that current code gives "Run-Time Error 9, Subscript out of range" and highlights copy/paste code.
I've found the solution.
My destination workbook was open in a separate window so the source wasn't recognising it as being open. Bit of a nightmare!
It's necessary to have both workbooks open in the same instance of Excel.
Additionally, my original paste code only pasted "#REF". I have changed that to -
Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1").PasteSpecial xlValues
Works much better.
One more thing, in case anybody might find it useful. The source workbook must be active in order to carry out the auto-update.
Adding below line sorted out most issues though its still a work in progress -
ThisWorkbook.Activate

Excel cell value editing when VBA program running

When Excel VBA macro excecuting, can we change values of cell in excel in which program is running?
Editing option i want to enable when script/program is running
When your VBA consists of a lot of loops you can still do things in excel by adding:
DoEvents
Add this inside the loop(s), this will allow for other events (such as the user changing stuff) to happen while the program runs.
Do keep in mind the warnings others already gave you, you can seriously disrupt your VBA script, because the user could actually mess with all the excel spreadsheet content that you might interact with in your script.
A little "fun" example:
Option Explicit
Sub example_DoEvents()
Dim timer1 As Date
timer1 = Now()
Do While Now() < timer1 + 10 / 60 / 60 / 24 'only let it last for 10 seconds
DoEvents
ActiveSheet.Range("A1").Value2 = Selection.Address
Loop
MsgBox "END"
End Sub

Excel VBA: Printing x copies each with an increasing number in a cell

Is it possible to either:
Increase a number in a cell each time a page has been printed: e.g. print 20 pages with numbers from 1-20 in the cell
or if that isn't possible:
Run a For-loop, each increasing the number and each time prompting the printer to print.
I know this is a very basic question, but bear with me - It is the 1st time I'm trying my luck at Excel macros.
Cheers
This code can be put into 'ThisWorkbook' object to increment a cell value each time something from the workbook is printed.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = _
ThisWorkbook.Sheets("Sheet1").Range("A1").Value + 1
End Sub
It is possible to use a VBA Macro to populate and print a worksheet. For Excel 2010, use the Developer ribbon to Record Macro. It will record the steps you want in a Visual Basic macro, which you can then edit. You can change the macro to use a For-loop, changing the worksheet each time before you print.

Resources