Too long saving time in Excel via VBA - excel

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

Related

Closing Excel via VBA

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

workbook.close crashes excel

I'm trying to open an excel file, print it and then close it by clicking on a button.
It opens, it prints, it closes my file and then excel just crashes.
No warning, it justs closes everythting (2 files) and restart by opening the "version 1" of my files.
Workbooks.Open "Thenameofmywb.xlsm"
Workbooks("Thenameofmywb").PrintOut
Application.CutCopyMode = False
Application.Wait (Now + TimeValue("0:00:05"))
Workbooks("Thenameofmywb.xlsm").Close SaveChanges:=True
Here is a bit of my code, I tried the Doevents method and a lot of others small things but none worked.
Maybe you'll be able to help me.
I found it. I don't think it was code related or not in the code I wrote.
The file that I was opening must have been corrupted.
I tested with "clean" files and it worked.
So I copied and pasted informations from the "corrupted" file to a new one and now it runs well.
Now I don't know how they got corrupted but that's for an other time.
Thank you all for your help
You are not making any changes to the file, just accessing it, printing and closing it. So last part should be SaveChanges := false
Also "On Error Resume Next" at the beginning of the sub procedure.
The below code works on my side, using Nathan's idea of using an object.
Sub qqq()
Workbooks.Open "C:\Users\info\Documents\HHH.xlsx"
Set wb = Workbooks(2)
wb.PrintOut
Application.CutCopyMode = False
Application.Wait (Now + TimeValue("0:00:05"))
wb.Close
End Sub
I tried even the following code and it works:
Sub hhh()
Workbooks.Open "C:\Users\info\Documents\HHH.xlsx"
Set wb = Workbooks(2)
wb.PrintOut
'Application.CutCopyMode = False
'Application.Wait (Now + TimeValue("0:00:05"))
wb.Close
End Sub
I commented out 2 lines of code. I am testing with xlsx as a file that needs to be printed. If you are using xlsm, there might be some code there that you have that could be causing an impact. You could still use xlsm but in a way that code from 2 workbooks open at the same time could cause an issue.

Inserting Row in Excel Takes a long time

I have an excel document (10 sheets). 1 sheet has at this point about 1600 rows and about 148 columns of information. There are no formula's in this sheet.
At first I thought it was a problem with my Macro. But it turns out manually inserting a row (or deleting) gives the same problem: a 14 second delay (for ONE ROW!). I've tried deleting all conditional formatting, Data Validation, (deleting hidden graphs that get there data from a different sheet). I've tried turning of Auto Calculation (which I also do in the Macro), I've tried inserting/deleting a row in "Design Mode"> But nothing seems to help.
One version of my file (I have different back-ups) seems to have fixed it. But I cannot reproduce this in my current file. I don't know what the fix was.
I've scoured the internet for solutions but have yet to find one. I'm running on excel 2010. Who knows the trick I need? Or is updating to excel 2016 my best option?
cheers!
The three primary causes of recalculation lag are calculation, event handling (due to a Worksheet_Change) and (to a lesser degree) screen updating.
Put this 'helper' sub procedure in a public module code sheet.
Public Sub appTGGL(Optional bTGGL As Boolean = True)
With Application
.ScreenUpdating = bTGGL
.EnableEvents = bTGGL
.DisplayAlerts = bTGGL
.AutoRecover.Enabled = bTGGL 'no interruptions with an auto-save
.Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual)
.CutCopyMode = False
.StatusBar = vbNullString
End With
Debug.Print Timer
End Sub
Use it like this,
sub main()
appTGGL btggl:=false
'do everything you have to do here
appTGGL
end sub
You can add or remove application environment settings as you see fit.
If the problem turns out to be a badly written Worksheet_Change then post it here or on code review for improvement,
quick update: somehow the problem is fixed when I remove to other sheets. These sheets are not connected by formula references in any ways. Both sheets have (flat) information implanted by macro's, but after the macro is done it's just flat information. I've tried disabling al relevant macro's but this didn't solve the issue. Only deleting the other sheets: If i deleted 1 of the sheets the time would go from 14 sec to 7 seconds, and if I deleted the other as well it would go to 0 seconds.
It seems as though somehow these sheets are connected by some kind of calculation (outside of the macros) but I cannot imagine what this might be. Any Ideas?

Excel crashes on opening a module in vba

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

Excel 2013 DisplayAlerts False is still prompting for changes save upon quitting

I have a macro that does some calculations then it is supposed to close excel withOUT prompting to save any changes and withOUT saving.
Here is the code:
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
Application.Quit
This works fine on Excel 2010. On Excel 2013 (64 bit version), This is also working fine. However, My friend using Excel 2013(32 bit version..not sure if it makes a difference) and the Excel is prompting him to save changes before closing.
Is there a different code I should be using for 2013? Any idea?
Thank you so much
This Save changes pop up appears in case if your excel contains any volatile functions as such. This function gets updated on workbook open and hence at the time of closing save changes dialog box appears. To suppress this you can use following code and i hope this will work:
Application.EnableEvents = False
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
Application.Quit

Resources