Link to home
Start Free TrialLog in
Avatar of mikensu12
mikensu12

asked on

Delete excel work Sheet by name

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

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use oBook.Sheets or oBook.WorkSheets collection but it only accepts index not name.
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial