要用VBA来关闭工作簿,用Workbook.Close 方法即可,而要退出Excel,则用Application.Quit 方法。
下面是一些代码示例: 关闭活动工作簿,如果工作簿有更改,提示是否保存:
Sub CloseWorkbook() ActiveWorkbook.Close End Sub
Sub ClostAndSaveWorkbook() ActiveWorkbook.Close Savechanges:=True End Sub
Sub CloseAllWorkbooks() On Error Resume Next Workbooks.Close End Sub
Sub QuitExcel() Application.Quit End Sub
Sub SaveActiveWorkAndQuit() ActiveWorkbook.Save Application.Quit End Sub
Sub SaveAllAndQuit() For Each wbk In Workbooks wbk.Save Next Application.Quit End Sub
Sub QutiAndNoAlerts() Application.DisplayAlerts = False Application.Quit End Sub
打开一个新工作簿
Workbooks.Add
Workbooks(1).Name
Workbooks.Count
Workbooks(2).Activate
Workbooks("Chap02.xls").Activate
ActiveWorkbook.SaveAs Filename:="NewChap.xls"
Workbooks(1).Close
ActiveWorkbook.Close SaveChanges:=False
Workbooks.Close
添加一个新工作表
Worksheets.Add
Worksheets(1).Name
Worksheets(3).Select
Worksheets(Array(1,3,4)).Select
Worksheets(“Sheet1”).Activate
Worksheets("Sheet2").Move Before:=Worksheets("Sheet1")
Worksheets("Sheet2").Name = "Expenses"
Worksheets.Count
Worksheets("Expenses").Delete