Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My code closes the workbook but not the window

Tags:

excel

window

vba

I have a problem with closing window. I am using excel 2013 and it opens every workbook in separate window. My code closes the workbook but not the window. Any ideas? I am calling this sub from another sub.

Sub export_sheet()
    Sheets(sName).Move
    ActiveWorkbook.SaveAs Filename:=sDir & sName & ".xlsx",  FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    Workbooks(sName & ".xlsx").Close SaveChanges:=True
    Workbooks(ThisWorkbook).Activate
End Sub

I have two Excel instances. One contains my code "temp 121015.xlsm" and the other one is just a empty window with the workbook name but the workbook is closed.

enter image description here

like image 646
Shan Avatar asked Oct 25 '25 04:10

Shan


1 Answers

Excel 2013's method of an independent application window for each workbook means that you have to work with the Application.ActiveWindow property to close the new workboook that you created without leaving an empty application frame.

Sub export_sheet()
    Sheets(sName).Move
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=sDir & sName & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close   '<~~ this closes the new workbook created by the .Move
    ThisWorkbook.Activate
    Application.DisplayAlerts = True
End Sub

You are already dealing with the ActiveWorkbook property created with the Worksheet.Move method. Continue working within this scope with ActiveWindow.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!