Unable to delete sheets using invoke code

Hi, I have a invoke code below:

Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook

xlApp = New Microsoft.Office.Interop.Excel.Application
wb = xlApp.Workbooks.Open(filepath)

xlApp.DisplayAlerts = False
For value As Integer = (TotalSheets + 1) To 20
TryCast(wb.Sheets(SheetNamePrefix & value.ToString), Microsoft.Office.Interop.Excel.Worksheet).Delete()
Next
xlApp.DisplayAlerts = True

wb.Save()
wb.Close()

and arguments:
TotalSheets = 17
filePath = excel path
SheetNamePrefix = “Team”

And an excel with sheets like “Team1” “Team2”

but i cant figure out why it wouldnt delete sheets “Team18” “Team19”

Make sure the sheets are not protected and hidden.

3 Likes

YOU SAVED MY LIFE.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.