★Excel.ApplicationオブジェクトのQuitメソッド
Set Book = ExcelApp.Workbooks.Add
ExcelApp.Quit ←解放
★ブックを閉じていない場合
Private Sub CommandButton4_Click()
Dim ExcelApp As Object 'Excel.Application
Dim Book As Object 'Excel.Workbook
Dim Sheet As Object 'Excel.Worksheet
Set ExcelApp = CreateObject("Excel.Application")
Set Book = ExcelApp.Workbooks.Add
Set Sheet = Book.Worksheets(1)
Sheet.Range("C3").Value = "こんにちは"
Book.SaveAs ("C:\Users\MM\Desktop\test\Case3.xls")
Book.Close ←解放
End Sub
★Excel.ApplicationオブジェクトのQuitメソッド
Dim ExcelApp As Object 'Excel.Application
Dim Book As Object 'Excel.Workbook
Dim Sheet As Object 'Excel.Worksheet
Set ExcelApp = CreateObject("Excel.Application")
Set Book = ExcelApp.Workbooks.Add
Set Sheet = Book.Worksheets(1)
Sheet.Range("A4").Value = "こんにちは"
Book.SaveAs ("C:\Users\MM\Desktop\test\Case4.xls")
Book.Close ←解放
ExcelApp.Quit ←解放
★暗黙の参照をしている場合
Dim ExcelApp As Object
Dim Book As Object
Dim Sheet As Object
Set ExcelApp = CreateObject("Excel.Application")
Set Book = ExcelApp.Workbooks.Add
Set Sheet = Book.Worksheets(1)
Sheet.Range("D2") = "VBとExcelは親和性が高い"
Book.SaveAs ("C:\Users\MM\Desktop\test\Case8.xls")
Book.Close
ExcelApp.Quit
Book.Close ←解放NG
ExcelApp.Quit ←解放NG
Set ExcelApp = Nothing ←解放
'変数宣言
Dim rng1 As Range
Dim rng2 As Range
'変数に値をセット
Set rng1 = Range("A1")
Set rng2 = Range("A2")
'値をセット
rng1.Value = "変更1"
rng2.Value = "変更2"
'メモリを解放
Set rng1 = Nothing
Set rng2 = Nothing
Set myObj = Nothing