1

I am trying to close the workbooks after finish executing macro. So i use this code

 Workbooks("RPA 1.xlsm").Close

However, after the RPA 1.xlsm closed, the other active workbook is freezed and can't be closed. It seems like something got stucked.

Anyone know why?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Raspi Surya
  • 315
  • 2
  • 11
  • 28

2 Answers2

1

Here's the answer for your question.

Closing Excel Application using VBA

Maybe the workbook still not saved that's why it freezes. Correct me if I'm wrong. Thanks.

Serversta
  • 93
  • 1
  • 1
  • 12
  • I'm not allowed to post a comment because I need 50 reputation. I said also that the workbook still not saved so maybe it's the problem. @EmanuelPirovano – Serversta Nov 08 '17 at 09:34
  • I tried using application.quit. However, all the excel file opened are closed. I only want to close the specific excel file – Raspi Surya Nov 08 '17 at 10:03
  • Usually when i first open another workbook then i'll go ahead and name it. `Set AnotherWorkbook = ActiveWorkbook` then when i'm done with it i just close it with `AnotherWorkbook.Close` – Serversta Nov 08 '17 at 10:51
0

Take this example :

Const xlAscending = 1
Const xlYes = 1
Dim filePath
    filePath = "C:\Users\book1.xlsx"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
Dim objWorkbook
Set objWorkbook = _ 
    objExcel.Workbooks.Open(filePath)
Set objWorksheet = objWorkbook.Worksheets(1)
Set objRange = objWorksheet.UsedRange
Set objRange2 = objExcel.Range("A1")
objRange.Sort objRange2, xlAscending, , , , , , xlYes
'objExcel.Application.DefaultSaveFormat = xlWorkbookNormal'
objWorkbook.Save
objExcel.Application.Quit
Set objExcel = Nothing

please conctrate on following :

 objWorkbook.Save
    objExcel.Application.Quit
    Set objExcel = Nothing 
 objExcel.Visible = False
objExcel.DisplayAlerts = False

Visible = false means that excel will not open

DisplayAlerts = false allow you to ignore msgBox that pop up when you try to change excel .

the first 2 commands are to close excel file.

hope all clear , let me know if you have more questions