简体   繁体   中英

Closing Workbook after opening it through VBA - Excel

I have the code below. I've called the excel document from a VBA code. However, after pressing the closing button on the workbook, it doesn't close until I stop the macro. I want to be able to close the document without stopping the macro. ie the main userform is still running behind. I can close the excel workbook from the close button top right of the workbook itself.

Anyone happen to know how to solve this?

Private Sub CommandButton7_Click()
  Workbooks("master system.xlsm").Activate
  Workbooks.Open ActiveWorkbook.Path & "\Toolbox\Define\PPST.xlsx"

End Sub

Try Workbooks("PPST.xlsx").Close SaveChanges:=True

This is from the documentation enter link description here

Try scattering DoEvents throughout your code after opening the workbook.

Just be aware that if you depend on the ActiveWorkbook or Selection objects in your code, they could change after DoEvents .

DoEvents essentially tells your code to pause, and let Excel do other, waiting tasks -- such as allowing the user to select cells, and probably closing a workbook -- before continuing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM