To use VBA to close the workbook, use the Workbook.close method, and to exit Excel, use the Application.Quit method.
Here are some code examples: Close the active workbook, and if the workbook has changes, prompts to save:
Copy Code code as follows:
Sub Closeworkbook ()
Activeworkbook.close
End Sub
If you want to avoid prompting, add the "SaveChanges" parameter, such as saving and closing the workbook directly:
Copy Code code as follows:
Sub Clostandsaveworkbook ()
Activeworkbook.close Savechanges:=true
End Sub
Change "True" in the preceding code to "False" to close the workbook directly without saving it.
Closes all open workbooks and prompts you to save:
Copy Code code as follows:
Sub Closeallworkbooks ()
On Error Resume Next
Workbooks.close
End Sub
Quit Excel and prompt you to save the workbook with changes:
Copy Code code as follows:
Sub Quitexcel ()
Application.Quit
End Sub
Save the active workbook and exit Excel:
Copy Code code as follows:
Sub Saveactiveworkandquit ()
Activeworkbook.save
Application.Quit
End Sub
The following code does not prompt you to save the workbook when you exit Excel.
Save all open workbooks and exit Excel:
Copy Code code as follows:
Sub Saveallandquit ()
For each wbk in workbooks
Wbk. Save
Next
Application.Quit
End Sub
Quit Excel and all changes are not saved:
Copy Code code as follows:
Sub Qutiandnoalerts ()
Application.DisplayAlerts = False
Application.Quit
End Sub
Some common operations for VBA are listed below:
Open a new workbook
Copy Code code as follows:
Get the name of the first workbook
Copy Code code as follows:
Get the number of open workbooks
Copy Code code as follows:
Activate a second open workbook
Copy Code code as follows:
Activate Workbook Chap02.xls
Copy Code code as follows:
Workbooks ("Chap02.xls"). Activate
The workbook that is currently active is Newchap.xls
Copy Code code as follows:
ActiveWorkbook.SaveAs filename:= "Newchap.xls"
Close the first workbook
Copy Code code as follows:
Closes the currently active workbook without saving changes
Copy Code code as follows:
Activeworkbook.close Savechanges:=false
Close all open workbooks
Copy Code code as follows:
If you run the last example, all of your workbooks are now closed.
Before you use it on a worksheet, make sure you open a new workbook first.
When you're in addition to a single worksheet, you have to know how to add a new worksheet to the workbook, know how to select one or a group of worksheets, and know how to name, copy, move, and delete worksheets.
In VB, each task needs a special method or attribute.
Add a new worksheet
Copy Code code as follows:
Get the name of the first worksheet
Copy Code code as follows:
Select a worksheet named "Sheet3"
Copy Code code as follows:
Select First, third and fourth worksheets
Copy Code code as follows:
Worksheets (Array (1,3,4)). Select
Activate a worksheet named "Sheet1"
Copy Code code as follows:
Worksheets ("Sheet1"). Activate
Move Sheet "Sheet2" to Sheet "Sheet1"
Copy Code code as follows:
Worksheets ("Sheet2"). Move before:=worksheets ("Sheet1")
Rename sheet "Sheet2" to "expenses"
Copy Code code as follows:
Worksheets ("Sheet2"). Name = "Expenses"
Get the number of worksheets in the current workbook
Copy Code code as follows:
Delete Worksheet "Expenses" in the current workbook
Copy Code code as follows:
Worksheets ("expenses"). Delete