ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to kill excel from visual basic (https://www.excelbanter.com/excel-programming/365075-how-kill-excel-visual-basic.html)

alexia_net

How to kill excel from visual basic
 
Hi
I am using the followig code. The thing is that excel won't stop. It is
something related to For...Next.
If I remove For...Next then everything is ok. What could be wrong?


On Error GoTo ErrorHandler

Dim MyXLApp As New Excel.Application
Application.DisplayAlerts = False
Dim strSheetName As String
strSheetName = "INFORMATION"
MyXLApp.Workbooks.Open FileName:="C:\yyy\1_08A.xls"



If MyXLApp.Worksheets.Count = 1 Then
MyXLApp.Worksheets.Add.Name = "DELETE"
MyXLApp.Workbooks.Application.SaveWorkspace
End If

If MyXLApp.Worksheets.Count 1 Then
For buc_sheets = 1 To MyXLApp.Worksheets.Count
den_sheet = Worksheets(buc_sheets).Name
If strSheetName = den_sheet Then
MyXLApp.Worksheets(den_sheet) Then
MyXLApp.Worksheets(strSheetName).Activate
MyXLApp.Worksheets(strSheetName).Delete
End If
Next
End If
GoTo line100
MyXLApp.Workbooks.Application.SaveWorkspace
line100:
MyXLApp.Workbooks.Application.SaveWorkspace
MyXLApp.Workbooks.Close
Application.DisplayAlerts = True
MyXLApp.Application.Quit
Set MyXLApp = Nothing
ErrorHandler:
Select Case Err.Number
Case 9
MsgBox Err.Description
End Select


Bob Phillips

How to kill excel from visual basic
 
Not tried it, but it may be because you are using auto-instancing..

Instead of this

Dim MyXLApp As New Excel.Application

Try

Dim MyXLApp As Excel.Application

Set MyXLApp = New Excel.Application



--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"alexia_net" wrote in message
oups.com...
Hi
I am using the followig code. The thing is that excel won't stop. It is
something related to For...Next.
If I remove For...Next then everything is ok. What could be wrong?


On Error GoTo ErrorHandler

Dim MyXLApp As New Excel.Application
Application.DisplayAlerts = False
Dim strSheetName As String
strSheetName = "INFORMATION"
MyXLApp.Workbooks.Open FileName:="C:\yyy\1_08A.xls"



If MyXLApp.Worksheets.Count = 1 Then
MyXLApp.Worksheets.Add.Name = "DELETE"
MyXLApp.Workbooks.Application.SaveWorkspace
End If

If MyXLApp.Worksheets.Count 1 Then
For buc_sheets = 1 To MyXLApp.Worksheets.Count
den_sheet = Worksheets(buc_sheets).Name
If strSheetName = den_sheet Then
MyXLApp.Worksheets(den_sheet) Then
MyXLApp.Worksheets(strSheetName).Activate
MyXLApp.Worksheets(strSheetName).Delete
End If
Next
End If
GoTo line100
MyXLApp.Workbooks.Application.SaveWorkspace
line100:
MyXLApp.Workbooks.Application.SaveWorkspace
MyXLApp.Workbooks.Close
Application.DisplayAlerts = True
MyXLApp.Application.Quit
Set MyXLApp = Nothing
ErrorHandler:
Select Case Err.Number
Case 9
MsgBox Err.Description
End Select





All times are GMT +1. The time now is 06:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com