I need to run my excel VBA daily at a specific time. I google the ontime method, however I dont get a comprehensive understanding from it. I would bring up this to clean up my confusion of ontime method.
Does the containing excel workbook have to be open for ontime method before it runs. If yes, Is there a way to open the excel workbook automatically at a specific time.I know it mite be done by Timer or a Windows task Scheduler.
Could someone work me through this. Is my current code properly constructed for automated task scheduler?
My current code looks like this:
Sub StartTimer()
Application.OnTime EarliestTime:=TimeValue("11:15:00"), Procedure:="rune", _
Schedule:=True
End Sub
Sub rune()
SourceOneUpdate
SourceTwoUpdate
SourceThreeUpdate
GenerateReport
End Sub
Private Sub workbook_open()
StartTimer
End Sub
This is based on the idea from this post: http://www.cpearson.com/excel/OnTime.aspx which mite be helpful. Even this workbook is open, its not running automatically. Could someone help me on this to see why this is not working properly.
Thanks in advance.
just to expand on d-stroyer's comment.
You first need to make sure your macro setting is enabled at all times because this will ensure that everytime your workbook opens, the macro runs without any notification or confirmation.
To do this,
Excel Options > Trust Center > Trust Center Settings (Button) > Macro Settings > Enable All macros > OK
Now that your macro is enabled, you need to ensure the macro will run as soon as the workbook opens. So, go to your VB editor and open the ThisWorkbook module > Create a workbook_open() event and copy and paste your "OnTime code" into workbook_open event > Save & Close
Now, everytime you open the workbook, the workbook should run the desired code at 8:47.
Go to Task Scheduler, on the right click on "Create Basic Task...". Give the task a name and click next. Select a trigger (Daily in the case you mentioned here) and click next. Set the time and recurrence period and click next. In the Action, select "Start a program" and click next. In the Program/script text box browse for your Excel file and click next (leave the other text boxes empty). Click on Finish. I just tried this on my PC now, and it works.
From Windows Help Forum
PS: Make sure you set the scheduler to open the excel file BEFORE 8:47 (so maybe 8:46).
Cheers,
kpark
EDIT: try running this to see if your OnTime is working..
Sub RunOnTime()
Application.OnTime Now + TimeSerial(0, 0, 10), "theSub"
End Sub
Related
I have two macros in in a Powerpoint file in Microsoft Office Professional Plus 2016 version.
1. Does saveas work
2. Closes Powerpoint
I run these two macros one by one using PowerPoint /m command line option. It's working fine. However, few times 2 macro close call is intermittently taking more time. When I move mouse on PowerPoint window after "close" macro is called, it closes instantly. I am pretty sure first macro saved the file and finished execution successfully. The close call macro code is below. I don't want to save any work. I don't have any other Powerpoint process running. Please advice how can I speed up this? I wonder what's holding Powerpoint. Thank you.
Sub closeit()
With Application.ActivePresentation
.Saved = True
.Close
End With
Application.Quit
End Sub
The delay in macro 2's execution is most likely caused by macro 1 finishing the save. It's not possible to run two macros at once, so 2 can't run until 1 is done.
What's the logic behind two separate macros? It seems unneccessarily complicated, you could just add this code to the end of #1.
I have a custom button (not an add-in, just a "shortcut" button) in Excel's quick access toolbar to run a macro in the active workbook. The workbook is modified and saved daily with a new filename. When the link is first created in the active workbook (call it Version 1) the macro runs fine. However the following day (filename now Version 2) clicking on the macro button opens workbook Version 1 and runs the macro saved in Version 1.
Is there a simple way to break the link and run the macro in the most recent active workbook?
The code is basic - it just opens up a userform and is saved in "ThisWorkbook"
Sub OPEN_DATA_USERFORM()
ufDATA.Show
End Sub
I will try to guide you through this, I hope it helps.
What we need to do is we need to call OPEN_DATA_USERFORM() from our "PERSONAL.XLSB" file. In VB editor screen, in the project explorer you will find a file called "PERSONAL.XLSB", you need to add an "Module" to that and add following code:
Sub KickOff()
Call Application.Run(Excel.ActiveWorkbook.Name & "!OPEN_DATA_USERFORM")
End Sub
By this we will be able to call the userform.show function from our PERSONAL.XLSB which is always running in the background when you open the excel.
PS: I am assuming your OPEN_DATA_USERFORM() is coded in your daily workbook.
UPDATE: PERSONAL.XLSB
Your file should be visible in your project explorer like below:
If it is not there record a dummy macro and select your personal file like below then it should appear in your project explorer in VBA screen:
Is it possible to run a macro on web browser? I can open an excel in a browser but i am not sure if macro can run with it. And also, how am I going to enable it automatically within a browser?
If I am correct in assuming that you're asking if a vba macro can be run automatically after opening it from a link on a webpage rendered by a browser, the best way to do that is to set code to run automatically whenever the workbook linked in the webpage is opened.
To have a macro run when a workbook is opened double click the "ThisWorkbook" object in the vba project explorer of the workbook that has your code and add these lines of code
Private Sub Workbook_Open()
Call Module1.hello 'Change this line to start your code
End Sub
This automatically executes the sub "hello" in Module1 of the workbook.
Note that the user has to have Macros Enabled in their Trust Center Settings for anything to run.
Also note that depending on their browser and where the actual link points to the workbook file might have to be downloaded first and then opened from the download location for the macro to run.
This may be a stupid question, but I feel like I'm in a pickle. I have a modal UserForm that opens when an Excel workbook is opened. When the UserForm is closed, the Excel workbook is saved and closed. I need to be able to view my code, but I can't seem to figure out how to do that because if I close the modal dialog box, the workbook closes. Does anyone know how I can view my code? I really apologize if this is a stupid question, but I can't seem to figure it out.
Thanks for you time and effort.
Without restarting the workbook i.e when the userform is shown in modal, you can use CTRL + Shift + Pause/Break to get into the VBE
Depending on laptops the key combination might change. Here is another which you can try.
Fn + Pause/Break
Hold the shift key when opening the workbook. This allows you to open office applications with macros not running and can be useful in situations like this.
Then view the macros (hit Alt+F11 to open it this editor).
The two other suggestions are good. For easiest debugging, I'd put the code that opens the userform in a separate routine and then call that routine from Workbook_Open. That way you can run and debug your code without having to re-open the workbook.
Then your ThisWorkbook module might look something like this:
Private Sub Workbook_Open()
MyUserformProcedure
End Sub
Sub MyUserformProcedure()
UserForm1.Show
End Sub
You could then comment out the line in Workbook_Open and call MyUserformProcedure, and uncomment the line when you're finished debugging.
In 2003 the workbook would just close. But now the same code is re-opening the workbook. There are some lines afterwards and then the sub ends. When the sub ends, the workbook_open event gets fired for the closed workbook, even though there is no code that opens any workbooks. The debugger is almost useless, it is not reproducing the bug if I execute everything step by step, in fact everything works fine when I use the debugger.
I use
ActiveWorkbook.Close False
For closing the workbook (from an add-in).
Help much appreciated.
I added a slight amount of code for Excel 2007 because of the annoying ribbon and Microsoft getting rid of custom menus. To somewhat compensate for this, I wanted to have the add-ins tab in the ribbon always visible for convenience, but I was using Application.OnTime and sendkeys to do it (because Microsoft didn't bother including an API with the stupid ribbon...). Well OnTime seems to have been the root of my troubles.
Figured out how to get both. Workbook_beforeclose cancels the scheduled ontime event by calling:
Application.OnTime EarliestTime:=Now(), Procedure:="Name", Schedule:=False
but otherwise the ontime event gets executed.
If the event does not get cancelled before beforeclose is called, the workbook will close then re-open to run the scheduled ontime event (because the workbook is opened so briefly that ontime doesn't get a chance to run before I need to close it).