Close Method of Workbook Object VBA

We can close workbook by using ‘Close method of workbook object in Excel VBA. It closes the workbook object. Please find the following brief description about close method of workbook object.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


Why we need to Close a Workbook using VBA?

Once we perform the tasks in Excel Workbook, we need to close the workbook. And the whenever we want we can reopen Workbook again.

VBA Close Workbook – Syntax

Here is the syntax to close workbook using VBA.

Workbooks(“Your Workbook Name”).Close(

[SaveChanges], [Filename], [RouteWorkbook])

In the above syntax we are using ‘Close’ method of workbook object to close the workbook.

VBA Close Workbook: Example 1

Please find the below example, It will take you through to close workbook where SaveChanges:=True.

Sub Close_Workbook()
Dim Wkb As Workbook
Set Wkb = Workbooks.Open("D:\Sample.xlsx")
Wkb.Close Savechanges:=True
End Sub

Explanation:

In the above example we have created variable named Wkb in the first statement. We have used Open method of workbook object to open specified workbook named ‘D:\Sample.xlsx’ and then assigned it to object named Wkb in the second statement. At last in the third statement we have used ‘Close’ method of workbook object to close above specified workbook. Finally we have mentioned ‘Savechanges:=True’, that means if we have done any changes in the worksheet it will save the changes.

VBA Close Workbook: Example 2

Please find the below example to close workbook where SaveChanges:=False.

Sub Close_Workbook()
Workbooks (“ D:\Sample.xlsx").Close Savechanges:=False 
End Sub

Explanation:

The above example is same as like example 1 which we explained in the above. The difference is we have mentioned ‘Savechanges:=False, that means if we made any changes in the worksheet it discards any changes that have been made to it.

VBA Close Workbook: Example 3

Please find the below example to close workbook where SaveChanges:=False.

Sub Close_Workbook()
ActiveWorkbook.Close 
End Sub

Explanation:

The above mentioned code closes the active workbook.

VBA Close Workbook – Instructions

Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:

  1. Open an Excel Workbook
  2. Press Alt+F11 to Open VBA Editor
  3. Insert a Module from Insert Menu
  4. Copy the above code for activating a range and Paste in the code window(VBA Editor)
  5. Save the file as macro enabled workbook
  6. Press ‘F5’ to run it or Keep Pressing ‘F8’ to debug the code line by line.
LIMITED TIME OFFER

Leave A Comment


Related pages


hyperlinks in excelexcel adoexcel rgb formulapivot table formulas excel 2007how to select entire column in excelmsgbox new lineexcel auto open macropastespecial xlpastevaluesexcell concatenatenested if function excel 2010unprotect workbook excelfonts with underlinesimple cutting worksheetshide and unhide columnsdml examplesort vba excelexcel vba progress barxl to xml convertercombobox rowsource excelexcel insert multiple columnsmacros function in excelexcel connect to mysqlis null vbaoffset vba excelvba macros in excelexcel append data from multiple sheetsmerging excel columnspassword log template excelexcel kpi dashboard templatevbscript excelscreen update vbaend xlup rowcreate pivot chart excel 2010interactive excel dashboarddashboards with excelfinding duplicate cells in exceltechnical interview questions for freshersadvanced excel spreadsheet templatesexcel vba formula with quotesvlookup for beginnersformula vlookup in excelis null vbadefine array excelcopy data from protected excel sheetexcel pivot table tutorialshow to draw pivot tablehow to delete macros in excelexcel vba project examplesexcel run macro on saveinsurance dashboard examplevba application runexcel vba subvba excel copy pasteoutlook application vbacheck box in excel 2007sas coding basicscosting sheet template excelexcel programming for beginnersmessage boxes in vbexcel vba blankfte calculator excelsimple vlookuphlookup excel 2010 examplehlookup in excel 2010visual basic codes for beginnerssql and vbaunprotect excel sheet without password 2007interview questions and answers for mis executivecheckbox tutorialformula match excelisna functionprotect cells excel 2007excel vba sortexcel macro rename filehow to activate activex controlexcel hiding columnsexcel formula for difference between two datesexcel vba data analysisms office 2007 interview questions answers pdfexcel presentation templatesvba workbookmicrosoft excel intermediate tutorialexcel vba column letterexcel dashboards templatesaccess 2007 msgboxadding a drop down in excel