Looking for a code to activate a function that i made called "IV" at 9:00 pm every day.
i tried following some other scripts but i can get it to activate the function using clock time.
I assume you're using windows.
You could use a sleep function to control the process, but when you run macros you can't access your sheets in excel. VBA is not really set up for this type of thing so I propose another method.
Open up visual basic and put this in Thisworkbook:
Private Sub Woorbook_Open()
yourmacro.IV()
ThisWorkbook.Close()
End Sub
Where you just substitute the names of yourmacro with the name of the macro file which contains your prodedure called IV.
This will run when the workbook is opened by the task scheduler, run the procedure, then close the workbook.
Then make a new task using windows task scheduler that opens this workbook at 9pm every night. (tutorial here: https://datacornering.com/how-to-run-excel-file-from-windows-10-task-scheduler/)
Related
I have an excel workbook with five different recorded macros on five different sheets. Is it possible to create another macro and run those five macros consequentially?
The difficulty is those macros are not specified to each worksheet. I have to run those macros each time manually.
Any thoughts are appreciated! Thank you.
As explained in this question: Multithreading in VBA cannot be done natively.
Can't be done natively with VBA. VBA is built in a single-threaded apartment. The only way to get multiple threads is to build a DLL in something other than VBA that has a COM interface and call it from VBA.
So running all 5 macros at the same time would require a lot of work.
But OP mentioned in the comment that running all 5 macros sequentially would be an option.
What you can do is:
Add a new module
Add a new public sub in this module
Reference all macro names in this sub
Example of how this macro could look like:
Public Sub allMacros()
macroName1
macroName2
macroName3
Sheet1.macroNameNotUnique
Sheet2.macroNameNotUnique
End Sub
Now running this macro will run all the specified macros sequentially.
It's a very simple question, but I can't find an answer to it anywhere.
Maybe some more information is necessary. I have a workbook with some procedures schedules in the future.
Now I know there is no easy way to get rid of the schedules, so I wonder if I were to copy the document, would the schedule be copied too?
With Application.OnTime you'll schedule procedures within the scope of the current Application object. E.g. if you close it, all active schedules will be cancelled.
It might be that the scheduled runs are set in a Workbook_Open event or something similar - In that case the schedule will be reinstated as soon as you open the (copied or original) document.
As implied by Zeretil his answer: the schedule will refer to the workbook's procedure when the schedule was created. When you copy and open the workbook, this will not change and the procedure in the copy will of course not run.
If you close the application with the original workbook, the schedule will also stop running.
Last but not least: There is an easy way to cancel the schedule in the current workbook, by using the Schedule parameter of the OnTime method:
To cancel the schedule for procedure "test":
Edit: Included schedule at variable time + cancel at the set time
Sub RunTestOnTime()
Dim eTime as Variant
eTime = Now() + TimeValue("00:00:20") 'Store the time in a variable
Application.Ontime EarliestTime:=eTime, Procedure:="test" 'Schedule it
Application.OnTime EarliestTime:=eTime, Procedure:="test", Schedule:=False 'Delete schedule.
End Sub
Complete documentation on the OnTime method here on MSDN
So because I am a very impatient man I decided to test it myself.
Here is the code I used:
While the "test" procedure was being executed correctly every 20 seconds, I copied and opened the copy of the workbook.
The schedule did in fact not get copied, so copying and replacing your document seems like the easiest solution to getting rid of OnTime events. Renaming your document to the original name does not cause the schedule to run on the copied document.
I want to create an excel template that will includes formulas for dating the columns. However, since those formulas will be based on TODAY(), I need to convert them to static strings (so the dates don't change everytime someone opens it). Is there a way to add a macro that will run automatically when someone creates a new spreadsheet based on the template? (Similarly to Auto_Open(), only on Create, rather than Open). If so, I could just create a macro that will replace the formulas with their results upon document creation. Can this be done?
[Note: I'm not married to this solution; it just seemed like the simplest way to protect my spreadsheet. If someone can suggest an alternative approach, I'd be obliged.]
I have a couple thoughts...
If you run a copy/paste values macro every time it really won't matter, right?
You could check if the file exists yet (has been saved), and if not
then this must be the template opened as a new workbook, maybe?
Code:
Private Sub Workbook_Open()
If Dir(ActiveWorkbook.Name) = "" Then
'run the macro?
MsgBox "I'm gonna run this macro"
End If
End Sub
You could have a cell on one of the sheets, that will never be used,
or is hidden, that will store whether or not to run the macro, and
change that when the file is opened or when the macro is ran. Then
have a macro run on open that checks that cell. (Or custom/document property)
You could populate the cells that have the today() formula only on
open and if they are already populated then don't run the macro?
I realized that there is no need for a Workbook_Create() function, as that behavior can be emulated by simply deleting the macro after it has run once (which happens when it is first created). Deleting macros is done automatically when the file is saved with a .xlsx extension. In addition, you need to prevent the macro from running when you open the template itself (while editing the it). You can hold the SHIFT key when opening it to prevent auto-run macros, but since that method isn't foolproof, I added this code at the top:
Private Sub Workbook_Open()
'If we're opening the template, don't run the macro
If Application.ActiveWorkbook.FileFormat = xlOpenXMLTemplateMacroEnabled Then
Exit Sub
End If
...
'add code here to SaveAs .xlsx, thus removing the macros, so it won't run every time.
End Sub
(Note: I didn't show my SaveAs code as it is rather messy: I wanted to suppress the default warning about losing macros, but wanted to also protect the user from inadvertantly overwriting previous file. If anyone is interested, I could post it)
I've got a Workbook_Open event macro (and it's in ThisWorkbook) that doesn't always run.
If Excel is closed and I double-click the .xls file from Windows Explorer, it does run.
If I launch Excel from Start/Programs (with no .xls file) and then open the file, it does run.
If I've already opened the file in Excel, but then close the file (leaving Excel open) and reopen it, then the macro does not run.
I've got security set to medium and I enable macros whenever it opens.
What do I need to do to get the macro to run whenever I open it, not just the first time for this Excel session?
(btw, this is Excel 2003)
I thought that this was the most cogent article on this problem (which is a long-standing never explained completely erratic bug that Excel exhibits). (dead link)
In short, in many cases it's a timing thing caused because the workbook is trying to calculate stuff when it opens and that gets in the way of the Workbook_Open event. The way to test on yours to see if that it for this situation, is to first rename any UDFs or Macros called by cells so that they won't get called and see if that changes anything.
I experienced the same problem.
I tested a workbook on my computer without any troubles. After destributing it to my customers I was told, that some combo-boxes stayed empty. These are usually filled from inside the workbook_open routine.
I tried different things to enable the workbook_open-Event - without success.
Finally, I found that disabling all userdefined Functions (UDF) lead to correct execution of workbook_open.
As my workbook is opened from another file, I will try to set calculation to manual first and then run the workbook_open manually. This may be done by defining it
public sub workbook_open
instead of
private sub workbook_open
Strange, that excel does not time this by itself...
A late answer (better than none).
I've had this problem now a few times (with Excel 2010).
The solution that has always worked (so far) was: remove conditional formatting, in particular if it contains UDF as conditions. As #LanceRoberts wrote in an above post, it's ultimately due to UDF calculations "overriding" the Open event, but I've found that those are particularly harmful if used in conditional formats.
I was experiencing almost identical behavior, and found that it is due to a bug that occurs if conditional formatting rules are erroring out. It turns out that if the conditional formatting rules are based on any setup by the macros, and that causes the conditional formatting to error, the Workbook_Open macro will not even attempt to run.
To test, make a copy of your file, and delete all conditional formatting from the workbook. Save and reopen. If it fixes your issue, then rework the conditional formatting rules to not depend on functions/values that will be broken before the Workbook_Open macro runs.
A few suggestions:
Try signing the workbook with a digital certificate. Add this certificate to the Trusted Certificates store then try again.
If this is machine-specific, try re-installing Office.
Make sure you have the latest service pack(s) applied.
I encountered the same problem, and I avoid it using the security settings.
I use the options settings then confidentiality center, then "params of confidentiality center" (sorry but its a translation of the french version :-p)
then "files approuved" or something like this.
And add the file containing the excel workbook in.
And its finnaly worked after that.
Looked everywhere and never find that solution.
Hope it'll help someone
This happens when a workbook is closed with an Application.EnableEvents set to false, and then you open another workbook within the same instance of excel opened. To avoid this, make sure that all of your processes that disable events, reenable them before terminating. Special attention to "End" commands, error handlers and "exit sub" sentences in the middle of your program.
What causes it is that your other archive, the one you openned first, have a Workbook_Open procedure; Excel doesn't excute it a second time.
This happened to me also and took me hours to figure out.
Turns out the TODAY() function in Excel was causing the problem.
Once deleted from my worksheet everything worked again. Very strange bug.
To add to the Arturo Llano post: The following code was used to monitor the Workbook_Open event and then run ProcessX whenever a workbook was opened.
ProcessX contained an End statement. The result was that it worked only the first time. The End wiped out AppX, so there was no further monitoring of events. Removing End fixed the problem. (Using End is bad practice anyway as it stops everything without any kind of cleanup or termination of other resources).
'Code in: Personal.xlsb ThisWorkbook
Public WithEvents AppX As Application
Private Sub Workbook_Open()
Set AppX = Application
End Sub
Private Sub AppX_WorkbookOpen(ByVal wb As Workbook)
'A 1-second delay to allow opening to complete before ProcessX starts.
Application.OnTime Now + TimeValue("00:00:01"), "ProcessX"
End Sub
I've got a VBA macro in an Excel 2003 spreadsheet and I'd like to 'install' it on a machine running Excel.
I created a 'Trusted Location' on the local machine, and I know how to copy the module to the existing workbook and assign a key combination to invoke it, but I don't know how to make the macro appear automatically when someone starts Excel. Help?
The simplest solution is ( both for xl2003 and xl2007 ) to copy your xls containing the macro to the path = Application.StartupPath from your client machine ( can build a simple vbscript installer which instantiates excel and retrieves this information ).
This way your macro will be available in any workbook opened since the xls files located in startuppath are loaded at excel startup.
I remember the old way was to save your macro in your Personal.xls
workbook - then it would be accessible
any time you opened Excel - has this changed for 2007?
That was a way. A better way was (and is) to create an Add-in, which you can then enable & disable via Tools->Add-ins. (An Add-in will remain enabled even if you close and re-start Excel). You can save any .XLS file as an Add-in (.XLA).
Within your Add-in you could just use an Auto_Open method:
Private Sub Auto_Open()
DoStuff
End Sub
...or, you could hook up the Workbook_Open event, as Ryan suggests.
Of course, since the user can disable the Add-in, you also want to do the reverse in Auto_Close (or in the Workbook_BeforeClose event).
Actually, if you use events it'd be better to use the Workbook_AddinInstall and Workbook_AddinUninstall events. Using those is slightly more "correct", and also has the benefit that the 'close' event doesn't fire if you close Excel and then hit Cancel when prompted to save.
I'm not sure what you mean by 'appear', but you can execute it automatically by calling it from
Private Sub Workbook_Open()
End Sub
in the ThisWorkbook object module. Is that what you're looking for?
I remember the old way was to save your macro in your Personal.xls workbook - then it would be accessible any time you opened Excel - has this changed for 2007?