0
votes

I have some problem with closing workbooks in my macro. I wanted to speed up my macro by using:

Application.ScreenUpdating = False
'my code
Application.ScreenUpdating = True

But when it finish, suddenly all workbooks which I have been using open together. I wanted to close them so I add * Workbooks.Close * lines. Unfortunately, error appears when VBA reaches this code line.It is because excel "can't see" my workbooks. When I close error windows and try to run closing lines once again everything works properly, because workbooks had enough time to open. Probably excel need some time to update and open all workbooks, so I should postpone my code. I have tried * Application.Wait * and * Do Until loop *, but it did't work. I also have tried to close those workbooks before screen updating but excel just ignored those lines.

My last code lines:

Application.ScreenUpdating = True

Application.DisplayAlerts = False

Workbooks(wb_1).Close
Workbooks(wb_2).Close

Application.DisplayAlerts = True

Thanks in advance for help!

1
are wb_1 and wb_2 filled when trying to close the workbooks? Or did they lose their value? If you use Workbooks(wb_1).Close False will close without saving or asking to save the workbook, change to True if you want to save the changes.Damian

1 Answers

0
votes

As Damian said, try to add:

Workbooks(wb_1).Close savechanges:=false
Workbooks(wb_2).Close savechanges:=false

or true if needed