I have been chasing my tail for weeks, with more hours "Googling around than I want to admit.
I have a large, complex analytical app within an Excel 2016 spreadsheet that captures SQL table data, queries email data and does a lot of stuff that I think is pretty cool. Users see sales force automation performance metrics and charts. All is well except for one thing. I cannot stop the "save data?" dialog box from appearing no matter what I do.
As a workaround I've put the spreadsheet on on a network ride and given users a shortcut that runs a VBscript to copy this spreadsheet to a hidden drive on the local PC and runs it. So if they save it, there's no worries as they aren't working with the original data. But as one would easily imagine, the load time is necessarily longer than it need be and users are confused by a message when I am telling them they can't save the data.
Net of a lot of different experiments, it seems like I've uncovered a bug in Excel (yeah, I know, this sounds lame even to me) as I cannot make the Application.DisplayAlerts = False. It just will not take.
See image here:
enter image description here
The image above (or at the above link as I haven't submitted enough question yet to embed images) is obviously taken from the Immediate Window when I was running the app. I entered the steps in the exact order shown. Note that I set Application.DisplayAlerts = False and then checked the value immediately afterwords and it was True.
Very weird. Is this a bug?
One last aside that is probably irrelevant; I was using .XLSB format because of the smaller footprint, much shorter load time and to get around PC setup issues with macros. But I've switched back to .XLSX to simplify the experiment.
To clarify: the code temporarily halts after each single-step in the debugger, resulting in Excel setting it back to True while idle. Here’s a way to verify this behavior:
In the VBE code-development window, open the Immediate window (Ctrl-G).
Temporarily insert the following code somewhere in your routine:
Debug.Print "..."
Application.DisplayAlerts = False
Debug.Print "Application.DisplayAlerts: " & Application.DisplayAlerts
Stop
Using the “Set Next Statement” tool (or Ctrl-F9), set the Debug.Print "..." statement to be the next one to execute.
Run the code form there by pressing F5 (Continue).
You should see this displayed on the Immediate window:
...
Application.DisplayAlerts: False
Now, with the code stopped at the Stop statement, type “?Application.DisplayAlerts” in the Immediate window, and press the [Enter] key. You will get:
?Application.DisplayAlerts
True
because Excel has reset it to True while the code is suspended.
Remove the experimental code.
Related
When I run a series of macros eventually excel loses focus and stops updating the screen this can occur looking at the screen or especially minimizing and doing something else on the computer. It can only be fixed by double clicking somewhere scrolling around maximizing and minimizing... but I can't seem to accomplish it in VBA I would hate to have to make an external macro to click into excel just to make the screen refresh.
Are you asking to see everything happen or do you want to just let it run and come back to it? You can use the code Application.ScreenUpdating = False to prevent the crazy jumping excel does and just come back as normal.
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.
I've had an intermittent problem for the last couple years and it's finally gotten to the point where I really need to try tracking down a solution.
I'm running a VBA script that takes various csv files that are spit out over the course of the day and chops them up for use in various other processes.
Every 20 minutes it opens a workbook with a 'Workbook_Open' block that does the work. When it finishes the job it closes the Excel application. It does this all day, every day.
MOST of the time this goes off without a hitch. OCCASIONALLY, it will hang up as it's trying to save a workbook. What I see when I catch it is the "Save as" box with the file path/name and the cancel button. It just waits there until someone clicks "Cancel" or the system fails from trying to open Excel too many times.
If I catch it and click the button then it will proceed on as if nothing abnormal had happened. Otherwise it just waits there for the user to click the button, as if it were any other msgbox/inputbox.
The weird thing is that the file is saved regardless of whether or not I click "Cancel" or kill the application or whatever. There doesn't seem to be any pattern to when it happens.
I've tried searching for similar issues but all my searches inevitably come up with results relating to system/application crashes. This isn't that. Excel will continue as normal, even to the point of continuing to save files in the way it SHOULD behave, if I click cancel.
Help?
I solved this problem by adding calls to DoEvents before and after the .SAVEAS or .SAVE calls. It's just two days, but this seems to have taken a process that I run daily multiple times a day (that creates multiple workbooks to a shared drive and never got through a day without this bug happening) to being a perfectly hands off process.
I have a bunch of VB script in Excel that ultimately opens up a new Word document and places a bunch of information in it. Everything works well, but I am having one annoyance. The annoyance is that sometimes the Word document will open in front of everything else and sometimes it will open behind everything else.
I have tried duplicating the results and it appears to be random. Sometimes I think there is something consistent as to why it does it and then something happens so my theory doesn’t hold true. I must have generated the Word document over 100 times trying different stuff and can’t seem to figure it out.
I do have three monitors, running Office 2010, and Windows 7. I am not sure if the three monitors would have any affect as to why it isn’t consistent.
What I want is for it to be visible in front of everything when it opens.
This is the code I am using to make it visible and I tried it at the end, the beginning, and then at both the end and beginning of where it creates the document:
wrdApp.Activate
wrdApp.Visible = True
Anybody have any suggestions?
What I want is for it to be visible in front of everything when it opens.
No need for APIs for this.
Instead of this
wrdApp.Activate
wrdApp.Visible = True
Use this
wrdApp.Visible = True
wrdApp.Activate
You can't really activate it if it is not visible. Also ensure that the last line in your macro before you destroy the object is wrdApp.Activate. You wouldn't want the wrdApp to loose focus because of any other code...
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.