VBA script to close every instance of Excel except itself

前端 未结 6 927
忘掉有多难
忘掉有多难 2020-12-04 00:07

I have a subroutine in my errorhandling function that attempts to close every workbook open in every instance of Excel. Otherwise, it might stay in memory and break my next

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 00:28

    try putting it in a loop

    Set ObjXL = GetObject(, "Excel.Application")
    do until ObjXL Is Nothing
            Debug.Print "Closing XL"
            ObjXL.Application.DisplayAlerts = False
            ObjXL.Workbooks.Close
            ObjXL.Quit
            Set ObjXL = Nothing
            Set ObjXL = GetObject(, "Excel.Application")  ' important!
    loop
    

提交回复
热议问题