Avatar of borgb002
borgb002

asked on 

How to delete a sheet with a specific sheet-name

I'm using VS2008, VB.NET using the Interop COM and I'm trying to delete a sheet that is named as "SHEET_TO_DELETE" from a workbook using the following code. However(obviously) the last line is not working syntatically.  What's wrong please?/How can I delete that particular sheet?




Dim excel As Microsoft.Office.Interop.Excel.Application
            Dim wb As Microsoft.Office.Interop.Excel.Workbook
            Dim ws As Microsoft.Office.Interop.Excel.Worksheet
            Dim rng As Microsoft.Office.Interop.Excel.Range
 
            excel = New Microsoft.Office.Interop.Excel.Application
            wb = excel.Workbooks.Open(FileToLoad)
 
 
            'Delete any sheets named as SHEET_TO_DELETE
            wb.Sheets.Delete("SHEET_TO_DELETE")

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment
PHFrench
SOLUTION
Avatar of Cem Türk
Cem Türk
Flag of Türkiye image

Blurred text
THIS SOLUTION IS 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
Avatar of borgb002
borgb002

ASKER

Still same problem. It is highlighting the line:
wb.sheets.delete(ws)

with the following error:
Too many arguments to 'Public Sub Delete()'
Avatar of Cem Türk
Cem Türk
Flag of Türkiye image

can you check if wb.sheets collection has a find method so that you can find the sheets index with a given name, then try to delete the sheet with the index
ASKER CERTIFIED SOLUTION
Avatar of borgb002
borgb002

Blurred text
THIS SOLUTION IS 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.
Avatar of PHFrench
PHFrench

I am having the same issue. Putting together the code samples above I have the code below. In debug mode it goes through the process without any errors but when I open the spreadsheet the sheet is still there. The spreadsheet is a macro enabled (.xlsm) sheet. Could that make a difference? And I am using version 15 of microsoft.office.interop.excel.

        Dim excel As Microsoft.Office.Interop.Excel.Application
        Dim wb As Microsoft.Office.Interop.Excel.Workbook
        Dim ws As Microsoft.Office.Interop.Excel.Worksheet


        Try
            excel = New Microsoft.Office.Interop.Excel.Application
            wb = excel.Workbooks.Open(strFileAll)
            ws = wb.Sheets(9)
            ws.Delete()
            wb.Save() 'Save the excel
            wb.Close() 'Close the excel
        Catch ex As Exception
            MsgBox(ex.ToString)
            wb.Close() 'Close the excel
        Finally

        End Try
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo