Home Excel Powerpoint Word Outlook Error Codes

VBA Useful Codes

Welcome to the World of VBA
Home > VBAUsefulCodes

Excel Macro to close a workbook with or without saving the changes


As you know on closing an opened workbook, there are two possibilities: 1. Close the workbook without saving all the changes which are not saved yet 2. Close the workbook without saving any of the unsaved changes It is very simple to do using Excel VBA. While closing if set the SaveChanges parameter to true then changes will be saved and if it is set to false then changes will be ignored. Refer the below code.

Sub closeWorkbook()
    Dim wb As Workbook
    Dim fPath As String

    fPath = "C:\....\myfile.xlsx"
    Set wb = workbooks.Open(Filename:=fPath)

    ' For Example:
    ' To close this workbook with
    ' saving the changes
    wb.Close SaveChanges:=True

    ' To close this workbook without
    ' saving the changes
    wb.Close SaveChanges:=False
End Sub


Subscribe

Enter your e-mail below and get notified on the latest blog posts.




Tags

VBA Outlook KPI Excel Alt+F11 Dashboards Macros Recording Graphs Automation Developer WaterFall Powerpoint Charts Pivot Tables Forecast Charts


Follow Us