Avatar of AlexPonnath
AlexPonnath
Flag for United States of America asked on

vb.net deleting excel sheet in workbook

I am using some very basic code to test my app where i open a Excel Workbook, count the nbr of sheets in beginning
then delete 1 sheet and then count sheets again and save. For some reason i can not actually delete sheet, the sheet
count is same before and after delete and after save the sheet is still in work book. Hope someone can see what i am doing wrong here

Dim oExcel As Object = CreateObject("Excel.Application")
Dim oBook As Object = oExcel.Workbooks.Open("C:\Users\Documents\acme.xls")

Dim oSheet As Object = oBook.Worksheets(2)

MsgBox(oBook.Worksheets.count)

oSheet.Delete()

MsgBox(oBook.Worksheets.count)

oBook.SaveAs("C:\Users\Documents\ACME-New.xls",)
oExcel.Quit()

Open in new window

Visual Basic.NETMicrosoft OfficeMicrosoft ExcelVB Script

Avatar of undefined
Last Comment
Shums Faruk

8/22/2022 - Mon
Shums Faruk

Hi,

Replace oSheet.Delete() to oSheet.Delete
Dim oExcel As Object = CreateObject("Excel.Application")
Dim oBook As Object = oExcel.Workbooks.Open("C:\Users\Documents\acme.xls")

Dim oSheet As Object = oBook.Worksheets(2)

MsgBox(oBook.Worksheets.count)

oSheet.Delete

MsgBox(oBook.Worksheets.count)

oBook.SaveAs("C:\Users\Documents\ACME-New.xls")
oExcel.Quit()

Open in new window

Shums Faruk

Try below:

Dim oExcel As Object = CreateObject("Excel.Application")
Dim oBook As Object = oExcel.Workbooks.Open("C:\Users\Documents\acme.xls")

Dim oSheet As Excel.Sheets = oBook.Worksheets

MsgBox(oBook.Worksheets.count)

oSheet(2).Delete()

MsgBox(oBook.Worksheets.count)

oBook.SaveAs("C:\Users\Documents\ACME-New.xls")
oExcel.Quit()

Open in new window

AlexPonnath

ASKER
Does not make a difference still doesn't delete it
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
AlexPonnath

ASKER
Tried also your new code and still same sheet count after delete and the saved workbook still has all sheets in it. Also tried deleting
differences index and it makes no difference
Shums Faruk

Try below then:
Dim oExcel As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim oBook As Excel.Workbook = oExcel.Workbooks.Open("C:\Users\Documents\acme.xls")

Dim oSheet As Excel.Sheets = oBook.Worksheets

MsgBox(oBook.Worksheets.count)

oSheet(2).Delete()

MsgBox(oBook.Worksheets.count)

oBook.SaveAs("C:\Users\Documents\ACME-New.xls")
oExcel.Quit()

Open in new window

AlexPonnath

ASKER
Still doesn't do it...
I enclosed the Excel shhet i am trying to do this with maybe there is something wrong with the doc
LSR.xls
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Shums Faruk

Please post your full code
AlexPonnath

ASKER
This is the complete code required to do what I am trying to do.
ASKER CERTIFIED SOLUTION
Shums Faruk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AlexPonnath

ASKER
Great job, that one drove me nuts
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Shums Faruk

You're Welcome Alex! Glad I was able to help.