0
votes

I'm using VBA in an Excel Macro that copies data from the active workbook to a second workbook, closes the second workbook and then closes the first workbook. The problem is the ActiveWorkbook.close false only closes the worksheets and not the workbook; Excel is still running. The two sections of code were tested separately and work fine; when put together in the macro the closing first workbook problem exits. My code is:

Sub SacoFieldUnitDataCopy()
' SacoFieldUnitsDataCopy Macro
' This macro copies the Saco Field Units Avail_Run Stat sheet to a new workbook.
'
ActiveWorkbook.RefreshAll
Call ConvertToValues("Saco Field Units Avail_Run Stat")
Sheets("Saco Field Units Avail_Run Stat").Select
Sheets("Saco Field Units Avail_Run Stat").Copy
ChDir "C:\Saco Units Avail_Run Stats"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
    "C:\Saco Units Avail_Run Stats\Saco Unit Avail_Run Data" & "  " & VBA.Format(Date, "MM-DD-YYYY") & ".xlsx", FileFormat:= _
    xlOpenXMLWorkbook, CreateBackup:=False
Call CloseAllOtherWorkbooks
Application.Wait (Now + TimeValue("0:00:03"))
ThisWorkbook.Close False
Application.DisplayAlerts = True


End Sub

Function ConvertToValues(Sheetname As String)
'Select Sheet
Sheets(Sheetname).Select
'Select All Cells
Range("B4:C26").Select
Selection.Copy
'Paste Special to remove formulas
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
'Put cursor back on cell a1
Range("a1").Select
End Function

Function CloseAllOtherWorkbooks()
Dim WBs As Workbook
For Each WBs In Application.Workbooks
    If Not WBs.Name = ThisWorkbook.Name Then WBs.Close False
Next WBs
End Function

Any help would be appreciated. thanks in advance.

1

1 Answers

1
votes

Well, but you are specifically asking Excel to close the Workbook, not the Object Excel Application per se. Try replacing this line

ThisWorkbook.Close False

With this:

Application.Quit

That is, assuming both are running on the same instance of Excel, which seems like it.