Private Sub cmdUseCells_Click() XlsMake True End Sub Private Sub cmdUseRange_Click() XlsMake False End Sub Private Function XlsMake(UseCells As Boolean) As Boolean Dim XlsApp As Object, wb As Workbook, ws As Worksheet Dim ExcelWasNotRunning As Boolean Dim y As Integer Dim fName As String Dim ErrMsg As String Dim msg As String On Error Resume Next 'Excelの起動 Set XlsApp = GetObject(, "Excel.Application") If Err <> 0 Then 'Excelは起動されていない ExcelWasNotRunning = True Err.Clear Set XlsApp = CreateObject("Excel.Application") End If If Err <> 0 Then ErrMsg = "Excelの起動に失敗しました。" GoTo Exit_XlsMake End If XlsApp.Visible = True Set wb = XlsApp.Workbooks.Add If Err <> 0 Then ErrMsg = "ExcelBookの新規作成に失敗しました。" GoTo Exit_XlsMake End If wb.Worksheets(1).Activate Set ws = wb.ActiveSheet ws.Name = "テスト" 'セルに値を設定 With ws For y = 1 To 10 .Cells(y, 1) = y * 100 .Cells(y, 2) = y * 200 Next y End With If Err <> 0 Then ErrMsg = "セルの値を設定に失敗しました。" GoTo Exit_XlsMake End If '縦計を設定 ws.Range("A11").Value = "=SUM(A1:A10)" ws.Range("B11").Value = "=SUM(A1:A10)" If Err <> 0 Then ErrMsg = "縦計設定に失敗しました。" GoTo Exit_XlsMake End If '罫線を引く If UseCells Then ' With ws.Range(Cells(1, 1), Cells(11, 2)) ' Cellsにws.をつけるのを忘れておりました。オブジェクトがなくてもエラーにならないMSのおおらかさにはつねづね感謝しております。 ' インスタンスが残るのはこれが原因のようです。 ' このご指摘は 山本さんからいただきました。山本さんありがとうございました。 With ws.Range(ws.Cells(1, 1), ws.Cells(11, 2)) .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone With .Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With End With msg = "Excelが残っています。このmdbを閉じるとExcelのインスタンスも閉じます。" Else With ws.Range("A1:B11") .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone With .Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With End With msg = "RangeプロパティにA1 形式を使った場合、Excelは削除されます。" End If If Err <> 0 Then ErrMsg = "罫線を引くに失敗しました。" GoTo Exit_XlsMake End If ws.Range("A1").Select Exit_XlsMake: If Err = 0 Then 'エラーがなければ「名前を付けて保存」 Do fName = XlsApp.GetSaveAsFilename Loop Until fName <> False wb.SaveAs Filename:=fName Else MsgBox ErrMsg & vbCr & Error wb.Saved = True End If wb.Close If ExcelWasNotRunning Then 'Excelが起動されていなかったので、Excelを終了 XlsApp.Quit MsgBox "Excelを終了しました" End If Set ws = Nothing Set wb = Nothing Set XlsApp = Nothing MsgBox fName & "Ctrl+Alt+Deleteで「プログラムの強制終了」を立ち上げてみてください。" & msg End Function