VBA code won't delete sheet

SiGill

Registered User.
Local time
Today, 09:19
Joined
Dec 22, 2008
Messages
72
I have some code to open an excel workbook in the background, run the macro in that workbook, delete a worksheet, then save as.

It all works perfect apart from the delete worksheet part. Can anyone help?

This is my code:

Public Sub RunExcelMacroOrSub()
'declare variables
Dim xlApp As Object
Dim xlBook As Object
'excel application stuff
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("C:/temp/book2.xls")
'run the macro
xlApp.Run "Macro1"
'delete Sheet1
xlApp.Sheets("Sheet1").Select
xlApp.ActiveWindow.SelectedSheets.Delete
xlApp.Range("A1").Select
'save file
xlApp.displayalerts = False
xlBook.Saveas ("c:/temp/hopeitworked.xls")
xlApp.displayalerts = True
'done
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
 
Try

Code:
Public Sub RunExcelMacroOrSub()
'declare variables
Dim xlApp As Object
Dim xlBook As Object
'excel application stuff
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("C:/temp/book2.xls")
'run the macro
xlApp.Run "Macro1"
'delete Sheet1
xlApp.Sheets("Sheet1").Select
xlApp.DisplayAlerts = false
xlApp.ActiveSheet.Delete
xlapp.DisplayAlerts = true
xlApp.Range("A1").Select
'save file
xlApp.displayalerts = False
xlBook.Saveas ("c:/temp/hopeitworked.xls")
xlApp.displayalerts = True
'done
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
 
perfect

of course its the "are you sure you want to delete" message that was stopping me, why didn't I think of that.

Thanks dbDamo
 

Users who are viewing this thread

Back
Top Bottom