?
Solved

Delete excel work Sheet by name

Posted on 2012-09-12
2
Medium Priority
?
505 Views
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
oBook.Worksheet.Delete()
end if


oBook.Save()
 oBook.Close()
  objExcel.Quit()

Open in new window

0
Comment
Question by:mikensu12
2 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38393014
You can use oBook.Sheets or oBook.WorkSheets collection but it only accepts index not name.
0
 
LVL 23

Accepted Solution

by:
irudyk earned 1500 total points
ID: 38393482
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
oBook.Worksheets(1).Delete()
end if

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 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