Excel 2007 VBA Workbook closing then reopening - vba

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

Related

Delay in closing Powerpoint while doing close via VBA Macro

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.

Closing any open userform

After a good search and some (over)thinking I came to the conclusion that I have no answer on what seems to be a simple question.
I have an excel document with many (20+) userforms in it. If you press a button (that is not in the userform, but just on the excel sheet) to start over again it should close any userform that's open at that moment.
I tried with unload me but of course I got an error when there wasn't any open userform.
Then I tried to add on error resume next thinking it would skip the line if there was no userform and therefore not giving an error but just continue what I want it to do. (opening a new userform).
It did indeed not give me the error anymore but it doesn't close any open userform as well (when there is one open).
So here I am, hoping someone here can help me as I don't know what to do. I could list up all of the userforms I suppose but it should be possible to go faster and automatically I suppose?
Some more info: It is never possible to have more than one userform open at the same time. // The button I want to create closes all the userforms if there are any and leads the user back to the main menu.
Thanks in advance!
KawaRu
Try calling the following when you want to unload all forms
Sub UnloadAllForms(Optional dummyVariable As Byte)
'Unloads all open user forms
Dim i As Long
For i = VBA.UserForms.Count - 1 To 0 Step -1
Unload VBA.UserForms(i)
Next
End Sub
This is hopefully the worst code that I have written in the last 5 years, but it will close any breathing form in Excel that you may have (and will kill any variables etc) :
Public Sub CloseAll()
End
End Sub
Use with caution!
From the MSDN End Statement:
The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code.
Code you have placed in the Unload, QueryUnload, and Terminate events offorms and class modules is not executed.
Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed.
Object references held by other programs are invalidated.
The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms.
Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.

Microsoft Excel 2013 ScreenUpdating fails

After running a macro for a prolonged period of time the ScreenUpdating in Excel totally stops working even when the macro finishes. I never set it to false but it behaves as if it is set to false and the only way to make Excel update the screen again is to close it out and reopen it. The VBA screen works however. I have tried to run DoEvents and ScreenUpdating=True but the Excel window does not update visually (it is not frozen). Is there anyway of clearing the ScreenUpdating buffer? I have tried manually setting it to false and true to no avail.
If you want to duplicate this error use this code
Application.OnTime (Now + TimeValue("0:00:05")), "macroName", (Now + TimeValue("0:00:10")), True
then I have the code copy and paste some values and I control when the loop stops with a countdown value in cell A1 for example.
I got this issue in my Excel 2013. For me, it was caused when I loaded a userform. It was very, very strange, however. After a lot of trial and error I found that what caused it was using a Sheets("MySheet").Activate command. I moved this command from running after I had shown and closed the form, to immeiately before I show the form (i.e., immediately when user clicks the 'show form' button.) This fixed it. Try fiddling with your sheets.activate commands.

View Code When Modal Dialog Is Open In Excel

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.

Excel OnTime Events Scheduler

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