I am pretty new to macros but I attempted building one, a pretty elaborate one in fact! After the last validation piece that I included in my code, excel crashes every time I try to open the module where i coded most part of my macro. This happens even when i try to save the workbook that contains this macro. I am able to open other modules within this workbook and any other excel workbook. The problem seems to persist only with one particular module after I added an if condition at the beginning of a subroutine. Basically, below is what i tried that I believe makes excel to crash every time I try to do anything with this macro:
Sub Process()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
If ThisWorkbook.Sheets("Main").Range("XFD2").Value <> 2 Then
MsgBox "Create reports first!"
Else
'Lot of other stuff
End If
End Sub
Every time I try to open this module, windows says Microsoft excel stopped working and then i get another pop that leaves me with no other option but to close the program. I have attached the screen shot of the message.
I searched online and got to know such problems arise when a program is installed that would crash with Microsoft program, but I did not install anything new and also checked the add-ins to excel in safe mode, I see no com add-ins.
I would have removed that last condition, but excel does not allow me to open the subroutine. I have invested a lot of time building this macro, any help would be much appreciated!
Your Macro should not be causing Excel to crash, either the way you are saving the file is causing the error or you have a problem with its installation.
And make sure you set those reset ScreenUpdating and DisplayAlerts to true,
Sub Process()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
If ThisWorkbook.Sheets("Main").Range("XFD2").Value <> 2 Then
MsgBox "Create reports first!"
Else
'Lot of other stuff
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Related
Long ago I had a macro that would open some workbooks, do some fiddling, then close - all fully automated, called from the commandline so I could batch file it and stick it in windows schedule to run when needed.
I've had call to do it again and of course no longer have access to those files (like four companies ago) so had a bash at recreating them, but stuck on closing excel.
ActiveWorkbook.Close SaveChanges:=True
This works to close the workbooks I'm updating, however, if I use it on the macro containing workbook, it closes the workbook within Excel, but leaves the instance of excel running (with no workbooks).
Application.Quit
This is what I think should be the right command, however this also throws an error:
Run-time error '1004':
Application-defined or object-defined error
However, it's odd. If I just run that command within excel on its own, there's no error, it only errors if the workbook calls the macro containing it.
As it's an auto-launching macro, I have an initial macro in ThisWorkbook that has the Private Sub Workbook_Open() which kicks everything off.
I'd read that Application.Quit can cause this error message when the workbook has an auto-launching macro and it's called from a module, rather than ThisWorkbook, so I moved the Sub QuitExcel() that I created, containing just Application.Quit but still get this error.
The suggested posts from SO when posting this gave a few things to try, like DisplayAlerts=False and saving the workbook, so I updated my quit sub to:
Sub QuitExcel()
Application.DisplayAlerts = False
Application.EnableEvents = False
ThisWorkbook.Save
Application.Quit
ThisWorkbook.Saved = True
End Sub
(and various permutations of that)
However I still persist in getting the same error.
Interestingly, if I click "debug" (rather than end, continue is greyed out) Excel still quits, and doesn't actually go to debugging.
Anyone have experience in what I'm trying to achieve, I just want to kill Excel after it's finished running, from auto-launching a macro?
Answer turned out to be relatively easy, I need to close my workbook before quitting excel and the closing macro can't be in the ThisWorkbook, it has to be in a module (otherwise when the workbook is closed, access to the rest of the code is gone).
So my command ended up being:
Sub QuitExcel()
ActiveWorkbook.Close SaveChanges:=False
Application.Quit
End Sub
Just needed a bit more googling and trial/error.
You should know that VBA code only run in a workbook context ThisWorkBook , that mean when you close it using WorkBook.Close, there will be no code to execute.
So, you only save then use Application.Quit
Do not close the workbook
For Each w In Application.Workbooks w.Save Next w Application.Quit
I have a report I'm in the process of automating, there are a couple of modules, one containing all the Queries that get run, e.g Queries.SQLReport1 is the first sub, and then the report that gets generated using that data is in Reports.GenerateReport1.
I then have another Module called Main, whose contents is:
Sub ReportGeneration()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Queries.SQLReport1
Reports.GenerateReport1
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Even though in my main sub, as well as the Query and reporting subs, I have Display Alerts = False, whenever a sheet is deleted, the Delete Sheet confirmation box pops up. This doesn't happen if running the individual sub itself, only when its being called by the main sub.
I guess I could put a pop up at the start that says 'Please confirm Delete whenever asked' but preferably they'd be supressed. Is there anyway to globally suppress the warnings until all the subs being called are run?
So I created a new Workbook, made new modules, and copied and pasted all the code into the new modules, without changing a single line, but now it works. I am going to chalk this one up to freak bug, but appreciate people who commented/answered.
Scenario
I am using a macro whereby I use Application.Visible = False to hide the workbooks. Also I use Application.Visible = True to unhide the workbook. At certain situation, I use Windows(ThisWorkbook.Name).Visible = False and Windows(ThisWorkbook.Name).Visible = True to hide and unhide only the workbook which contains macro.
Problem
I noticed during these operations, some additional excel windows(without any workbook) appear other than the workbook. Please see the picture below. You can see a grey window behind with a name Excel. That is the window I am talking about
If I closed that window, the whole excel will close. Does anyone know why this extra window appearing and how to prevent it from appearing?
I am not sure if this will meet the needs of your specific situation. But, what if you kept Application.Visible = False at the beginning of your code and changed Application.Visible = True to
Application.Windows(ThisWorkbook.Name).Visible = True at the end. This worked for me.
Hiding Excel
With the following code
Sub AppVisibleTrue
Application.Visible = True
End Sub
Sub AppVisibleFalse
Application.Visible = False
End Sub
you are showing or 'hiding' the 'whole' excel application, so you have to 'unhide' it in the same code, otherwise you won't be able to use the open files after you hide it, e.g. open a new workbook, in VBE add a new module and paste the above code into the module. Now, stay in VBE!!! Run the 'False' Sub. You will notice Excel has 'vanished', but you can still find it in the Task Manager's processes. Now run the 'True' Sub. Excel has 'reappeared'.
The following process will make Excel 'vanish'. The only way to close it will be via the Task Manager. If you not too familiar with doing this, just take my word for it.
Close the VBE. Now run the 'False' Sub. Excel has 'vanished'.
To conclude, this is obviously an error in your code, so I would suggest if you want to show a window (worksheet) that isn't 'ThisWorkbook' and you're dancing from one to the other, you should declare a variable
Const strSheet as String = "Sheet2"
Dim oSheet as Worksheet
'...
Set oSheet = ActiveWorkbook.Worksheets(strSheet)
Now you do with oSheet whatever you want.
Try to lose Active, Select and similar methods in the code.
If you would provide the actual scenario with the codes, a better assessment of the problem could be done.
The additional Excel window(s) could be related to your Windows Explorer. If you are previewing a document (in this case an Excel document) then the application to view that document is running in the background.
In this case, forcing the application to be visible will also make the background "preview" windows visible.
Some unusal phenomenon started affecting my macro. I have a relatively big (13MB) excel workbook. ThisWorkbook.Save worked just fine. It took like 10 seconds to save the workbook (without any data changes). Now it takes for 2 minutes. Same goes for ThisWorkbook.Close SaveChanges:=True. I am using Excel 2010 on Windows 7.
The strange thing is I can save the workbook manually without any issues, like CTRL+S or using the Save icon in the Excel menu. This way the saving procedure takes less than 10 seconds just as my code used to do it.
The occurence of this unnecessary delay is really bugging me. Do you have any experience with an issue like this? I'm looking for tips to get an approach on the problem.
EDIT:
I've changed to xlsb format as was advised and went a little further with testing. There are some cases when the saving time looks appropiate:
Significant data changes in lots of sheets (48s)
The Save command is triggered from a newly created module without any other changes (5s)
All my charts (150pcs) are deleted (1s). Apparently each chart gives about 1 extra second to the saving time. I know, i should use one dynamic but the damn label bugs... And it was totally fine before!
EDIT 2:
The issue has vanished today. Save command works properly again, saving takes for only a few seconds. Dunno what caused the problem, but I am affraid it will be back and will affect my macro some day again. Excel is definitely quirky!
Since the manual CTRL+S works, I go with the SendKey method until there are better solutions. It does a much faster job than the Save command. Bugs like this are just killing me ...
ThisWorkbook.Activate
SendKeys "^s"
DoEvents
ThisWorkbook.Close
How about this?
Sub Macro1()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
ThisWorkbook.Save
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
I had similar issue and been struggling with it for a while. Even for every small files 0.5MB it took almost 1 min to save.
SendKeys doesn't work for me in 100% cases. Sometimes workbook was closed Thisworkbook.Close SaveChanges:=False before SendKeys was able to perform action ("^s"), even after adding Application.Wait Now() + TimeValue("0:00:05").
Somehow it's relating (in my case) to Application.CalculateBeforeSave property. When changed to False workbook saved in couple of seconds.
Dim oWB as Workbook
Set oWB = Workbooks.Open(main_path & file_name, False, False)
oWB.Sheets(1).Calculate
Application.CalculateBeforeSave = False
oWB.Save
Application.CalculateBeforeSave = True
oWB.Close savechanges:=False
set oWB = Nothing
I have an Excel VBA method (I didn't write it) that runs and one of the first things it does is hide the Excel session Application.Visible = False.
However, when the method has finished, it does not unhide the Excel session so it remains open and listed in the Task Manager but is hidden and seemingly unusable.
Does anyone know, without have the VBE open (so one can access the Immediate Window and run Application.Visible = True), how to unhide this Excel session? At the moment, I'm simply having to kill the session using the Task Manager.
This isn't a massive deal but I'm just interested if anyone knows how to resurrect such a session.
Like I said, it's not a big deal but was just interested if anyone knew of shortcut key or anything to bring it back.
There is no shortcut as such that I am aware of but you can do this.
Open MS Word and paste this code in the VBA Editor. Close all open instances of Excel which are visible and then run and this code. This will make a hidden instance visible. Manually close the instance and repeat the process if there are more instances.
Option Explicit
Sub Sample()
Dim oXLApp As Object
'~~> Get an existing instance of an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
On Error GoTo 0
oXLApp.Visible = True
Set oXLApp = Nothing
End Sub
I am not deliberately using a loop as the hidden instance can have a workbook which you might like to save?
If you want you can convert the above code to a VB Script document which you can directly run from the desktop.
Unfortunately, I don't have the control to make the changes required.
What do you exactly mean? Is the VBA Password Protected? If no then my suggestion is still the same as earlier
This is a case of poor programming. Even if we give a code to close
all hidden Excel instances, that won't help you. Because next time you
run that macro, you will face the same problem again. Why not edit the
existing code and add Application.Visible = True at the end? Is the
VBA password protected? – Siddharth Rout 28 mins ago
A good solution!
Open up Word, assuming you have it, and open the VBA Editor there, then open the Immediate Window (Ctrl+G) and type:
Getobject(, "Excel.Application").Visible = true
and press enter.
I had a similar problem and solved it with code line reordering.
Look for a line like this ActiveWorkbook.Close that might be the reason you cannot unhide the session.
If you can find it, put Application.Visible = True just before it and voila.
as code:
sub runthis()
dim xl as object
set xl = new excel.application 'create session
xl.workbooks.open filename:= "«yourpath»" 'open wb in the new session
xl.visible=true 'this is what you need, show it up!
'rest of the code
end sub
No need for word macro at all.
Open up another excel workbook.
Hit Ctrl+F11 to go to the VBA editor and there yoy will see the running but hidden excel file on the left.
Search the code of the hidden application file for Application.Visible = False and comment it out. Save and restart the file.
Alternatively you can get back the application to show without closing if you type Application.Visible = True in the immediate window (Ctrl+G)