Excel - opening file stuck on 100%, until VBA code finishes - excel

I have Workbook_Open event, which fires a script with lots of code. It runs very long (getting data from web) and I would like Excel to open the file while it runs, this VBA also shows progress on status bar. I use loops and DoEvents has been used on each iteration with no effect. Only when macro executes successfully or I use Ctrl + Pause Break, file opens. Any ideas what can be wrong and how it could be fixed?
Edit: I didn't attach the code, as I am not the author and there are hundreds of lines in it. I had hope of finding a general cause why such situation would happen, from someone who encountered similar.
Important: it appears that the initial version of the file which I received from colleague works correctly until I save the file, so there isn't any "bug" in the code. Using VBADebugger didn't help. There are no external connections and names in the file.

I solved the problem by letting VBA excecute Workbook_Open event ASAP. I put the whole earlier content of Private Sub Workbook_Open under "scraper" procedure and now it looks like this:
Private Sub Workbook_Open
Application.OnTime Now + TimeValue("00:00:01"), "scraper"
End Sub
It must be that Excel won't (sometimes?) open the file before executing Workbook_Open event.

Related

VBA - Excel Delay and Forcestop a macro at opening

I got a file in task scheduler which has a macro that triggers opening like this:
Private Sub Workbook_Open()
Call OpenRunMacroandCopy
End Sub
A bit like a house alarm, I'd like to have this macro wait before executing the Call for a set duration so I could use ctrl+Break which i believe would interrupt execution and let me read the file and close it without OpenRunMacroandCopy running at all.
I know of Application.Wait and Sleep but they don't work here and i do not know why.
I also used Application.OnTime Now + 5 / 1440, "OpenRunMacroandCopy" which does seem to make it wait but ctrl+Break gives me no notification and i've had that macro run anyway so i'm not sure interruption is working? When I use ctrl+Break on an infinite loop macro i do get an error window which is quite nice so i definitely know i have it stopped.
Got an idea, or a more purposeful way to make that happen?

Excel add-in - get workbook name of "thisworkbook"

I can't get the name of the workbook when I use a add-in.
I'm trying to develop a add-in that runs each time you open Excel and reads the filename of the open file.
If the file name is XCFIL.SKV then do something...
This code should do it, but it doesn't. What am I missing?
The code stops and if I debug and press F8 it works fine, but it won't run on it's own.
Private Sub Workbook_Open()
If ThisWorkbook.Name = "XCFIL.SKV" Then
MsgBox "y"
End If
End Sub
Background:
Based in this statement The code stops and if I debug and press F8 it works fine, but it won't run on it's own. I assume the problem relies on the speed of the processor that is not sync with the code (own experience).
Solution:
Since it is Excel and the problem seems to rely only in the opening of the instance itself, you may use Application wait or any of the other functions for this matter.
Further thoughts:
Life cycle comes to my mind in these kind of scenarios. This web page has a neat Lifecycle diagram of excel instance (attached since I will try to explain the scenario)
As you may see "Application" is the first cycle of the Excel application, followed by "Open" and after that "Workbook" object, it may happen that in this life cycle "worbook" has not been created when "Open" comes to play,hence, you need to wait until excel solves this.

Workbook_BeforeClose not executing after using VBA editor

As the title suggests, I am having a problem with Excel's Workbook_BeforeClose event handler. I know I am using the correct method signature (shown below) because it has worked in the past, and I have correctly placed the event handler in the "ThisWorkbook" module of the VBA editor. However, I have noticed that whenever I do a significant amount of coding within the VBE (in other modules), this method is not executed. Even when I place breakpoints inside, the breakpoints are not hit before the workbook closes. Again, this only happens if I have been messing around with other modules in the VBE; if I just open the workbook and immediately close it, this method runs fine and the breakpoints get hit. Saving or not saving the workbook does not seem to make a difference, and this problem occurs in Excel 2013 and 2016 (haven't tried opening the workbook in earlier versions). This issue is really starting to impede my workflow, so if anyone has a thought on why my Workbook_BeforeClose handler is not being called, I would very much appreciate the help!
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'More code...
End Sub
After posting this question on the Mr. Excel forum, I was given the solution: Some of the other modules that I was editing contained optimization code. This included disabling Application events while the macro was running, then turning them back on after completion. However, while debugging, I would obviously occasionally stop the macro before it completed, so Application events were never being turned back on!
So now, all I have to do is execute "Application.EnableEvents = True" in the Immediate Window before I try to close the workbook, and my event handler runs fine!

VBA Form disappearing

I have a VBA form that when I click on it, performs some long calculations (a few seconds to several seconds long) and then displays the results on frames in the form. However, once every so often, the form hides on me and I need to click around to the VBA editor and back to the sheet to make it display again
I have
Me.Repaint
at the end of the calculations on the form
but it still doesn't help
I also tried disabling the "EnableCalculation" attribute of the main sheet, but still no use
Anyone ever run into something like this? Do I need to load the form in some special way?
I have had the same problem. When saving an Excel workbook by clicking on a command button on a modeless Userform, the form disappears as the code completes.
I read that the problem was due to some inter-compatible changes that Microsoft has made and found some very elaborate solutions involving a lot of complex code that I was unable to integrate into my project. However, I did find a very quick workaround.
In my case the Userform only disappeared when the code executed the End Sub command of the command button's code. I simply added an Exit Sub immediately before it and the Userform no longer disappears.
Have you tried turning off and on screen updating so Excel is not redrawing the screen with each change.
sub doSomthing()
Application.ScreenUpdating = False
'do something
Application.ScreenUpdating = True
end sub
The form disappears because Microsoft has changed the model by which forms are owned by a 'superior' process (Excel in your case). I have the same problem and also cannot overcome it.
Just sit tight until Microsoft decides that it is worth their effort to fix yet another introduced and untested problem by making changes to something that already worked.
The problem is a side effect of the SDI (Single Document Interface) that was introduced with Excel 2013. The various suggestions that invoke DoEvents, ScreenUpdating, etc. may help in certain scenarios, but they do not cure the behavior.
There is a full solution posted here: Keeping Userforms on top of SDI windows in Excel 2013 and up
If that is too involved for your needs, a quick-and-dirty solution is to minimize and restore the active window, which forces the userform back on top of the stack.
'Do long-winded process first, then:
Application.ScreenUpdating = False
ActiveWindow.WindowState = xlMinimized
ActiveWindow.WindowState = xlNormal
Application.ScreenUpdating = True
This doesn't prevent the form from disappearing, but it makes it reappear the end of the process.
Try experimenting with the following two lines of code instead of the repaint.
Me.Show
And/Or:
Me.SetFocus
Hopefully, one of these will work for you!
In my case I had to use all these to make it happen. (frmGUI is my form)
DoEvents
frmGUI.Repaint
frmGUI.Show
With Office 2013 and Windows 7, simply adding DoEvents right before the end of the sub (where the form disappeared) kept the form open. Nothing else needed....for my situation.

Form Show method in event workbook_open in ThisWorkbook breaks on focus

When I try to show userform1 with userform1.show in ThisWorkbook within the private sub workbook_open(), it does the strangest thing. (I'm using Excel 2007)- It enters break mode and stops the running of the code!!!
I open the macro enabled workbook and the userform appears as planned, but the when I move the mouse within the area of the userform it enters break mode and highlighting the row UserForm1.Show as if it is the problem.
Furthermore when I press F8 it's highlighting the private sub workbook_open() and another press highlights userform1.show again and another press shows me the run time error '400'.:
application-defined or object-defined error.
This has never happened to me before, I found a post that says it has no answer here.
Any ideas?
I also came across the same scenario.
What I did was to change the ShowModal property in the Form properties to False and the break went away.
I had the same problem, and the answers above didn't fix it. Restarting the PC resolved it temporarily, but after a while the problem returned. After a few frustrating days with a lot of restarting the pc, I came up with these conclusions:
What I think causes the problem is when you use ctrl+break to stop the code while running.
What solves the problem for me is to press ctrl+break while the code is NOT running, it'll show that it's in break-mode, and then press F5. It won't start running, but it will exit break mode again. When you've done this the problem should be fixed and you can run the code as usual.
I guess this method works the same as restarting your pc, but it's a lot faster.
It's an odd behaviour I already faced some times when dev'ing... some tips that might help you out:
Compile the code
Clear all breakpoints
Check project references (missing references may cause problems)
Add some unnecessary statement (like if 1 = 2 then DoEvents) before the 'hidden breakpoint', compile the code and then remove the code again
Install & run VBA Code Cleaner
Either way, that's a mystery for me the cause of this odd issue. It seems that somehow some breakpoints are kept in the memory even after removing them...
Hope it helps!
First thing to try is to recompile your VBA project and then save the workbook containing the macro. If compiling throws up an error you will probably get more information from the compiler message such as a missing reference.
Also make sure that you cleared all previously existing breakpoints. It might be the case that you first have to create a new breakpoint (F9) and then clear all breakpoints (Ctrl+Shift+F9) for the command to be enabled.

Resources