98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Access VBA Training and help » Macro running in the background, doing a task once a minute
Macro running in the background, doing a task once a minute
Resolved · Low Priority · Version 2016
Atanas has attended:
Excel VBA Advanced course
Macro running in the background, doing a task once a minute
Hello,
In the advanced course I did with Jens some time ago he showed how one can have a macro that runs all the time in the background and performs tasks without preventing the user from doing other things in Excel (or at least that's my memory).
I am hoping that someone could share a simple macro of this type, whereby once started the macro remains 'active' in the background and all it does is periodically, says once a minute, it copies a range and pastes it as values in a new location. In my case, this could be to keep track of highest or lowest prices on a security, i.e. if the current live price is higher than the stored one, replace the stored with the current, otherwise leave the stored one as it is. Ditto for lowest price. So the question is about getting Excel to run this macro in the background without stopping me from working on other tasks. many thanks.
Regards,
Atanas
RE: Macro running in the background, doing a task once a minute
Hi Atanas,
Thank you for the forum question.
You must think about Timed macros.
If you want to activate the macro when you open the file you will need to store a Call code in a open workbook event (In the visual basic editor double click This workbook in the project pane and select Workbook from the dropdown left above the module).
Type:
Call MyTimedCode
inside the Open workbook event.
Now in a "normal" module type:
Sub MyTimedCode()
Application.Ontime Now+Timevalue("00:01:00"), "DoSomething"
End Sub
Sub DoSomething()
"Type the code here you want to execute every minut"
End Sub
This code will execute a macro called Sub DoSomething every minute after you opened the file.
I hope this makes sense.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: Macro running in the background, doing a task once a minute
Hi Jens,
Many thanks for the quick response. A couple of questions if I may:
- Where is this 'Open workbook event'?
- For my education: do I need to place the macro there so that it keeps running? I mean, there is nothing in the macro itself to make it loop back after the first run. Is the placement in this event that causing this looping?
- How can I insert an on/off pause switch for the macro? E.g. immediately after the timing condition, I could insert a check on the value of a cell - if it is 1, continue, if zero then End. But instead of end, I would prefer just a temporary pause, i.e. skip the 'do something' bit.
- Lastly, how does one truly stop the macro from running, I mean from within the spreadsheet? Can one insert a kill switch in addition to the pause switch?
Many thanks again!
Atanas
RE: Macro running in the background, doing a task once a minute
Hi Atanas,
If you click on the link below you will find a very good explanation about ho to create open workbook events.
https://www.excel-easy.com/vba/events.html
You will not need to put any code in a open workbook event to do what you want, but by calling the timed macro from the event the repeating task will start automatically when you open the file.
You can also start the process by executing the MyTimedCode.
Sorry I forgot an important line in my answer earlier. I didn't call MyTimedCode in the DoSomething macro.
Sub MyTimedCode()
Application.Ontime Now+Timevalue("00:01:00"), "DoSomething"
End Sub
Sub DoSomething()
"Type the code here you want to execute every minut"
Call MyTimedCode
End Sub
If you want it to stop if you have 0 in lets say A1:
Sub DoSomething()
"Type the code here you want to execute every minut"
If Range("A1")=0 then
Exit Sub
else
Call MyTimedCode
End If
End Sub
It is more complicated if you want pause. You will have to use a Worksheet Change event, which will execute every time the cell with 0 or 1 change value. We did a worksheet change event on the course. We told Excel to calculate when something was changed in a range.
Please have a look at the linked below. This explain how to create Worksheet change events.
https://docs.microsoft.com/en-us/office/troubleshoot/excel/run-macro-cells-change
To Kill the code. If you in the change event add a If testing let say
if Range("b2")="Kill" then
Exit sub
End If
Then by typing kill in B2 you stop the code.
I hope this makes sense.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Access tip:Creating Parameter wildcard queriesTo creat a parameter query that also uses a wildcard, in the query design type in like []+*. |