Delete excel work Sheet by name

Posted on 2012-09-12
Last Modified: 2012-09-24
How do I delete the sheet by name.  The   oBook = objExcel.Workbooks.Open(FilePath & GetFileName  Date1 & ".xlsx")  works.

Dim objExcel As Excel.Application
        objExcel = New Excel.ApplicationClass
        Dim FilePath As String = "C:\ExcelReport\"
     GetFileName As String = "Book1.xlsx"

        Dim Date1 As String = Date.Now.Year.ToString & Date.Now.Month.ToString & Date.Now.Day.ToString

        Dim oBook As Excel.Workbook, oSheet1 As Excel.Worksheet, oSheet2 As Excel.Worksheet

     oBook = objExcel.Workbooks.Open(FilePath & GetFileName  Date1 & ".xlsx")
if oBook.Worksheet.Name = "Sheet1" then
end if


Open in new window

Question by:mikensu12
    LVL 83

    Expert Comment

    You can use oBook.Sheets or oBook.WorkSheets collection but it only accepts index not name.
    LVL 23

    Accepted Solution

    It looks like you are opening a workbook and want to delete the first sheet in the workbook if it is called/named Sheet1.  If so, try changing line 11-13 of you code to something like:
    if oBook.Worksheets(1).Name = "Sheet1" and oBook.Worksheets.Count>1 then
    end if

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now