Delete a sheet in excel?

faq

#1

how to delete a sheet in excel using uipath commands and save the file…


#2

Hi @shrganes,

Please refer below post. @ddpadil

Regards,
Arivu


#3

@shrganes, Refer this,
Delete Sheet

Regards,
Dominic :slight_smile:


#4

thank you…

i am able to delete it… but once i reopen the file the sheet still exists…how to over come that…??


#5

Hi @shrganes,

Try After delete save the excel file. Using send hot key

ALT+H+D+S

Regards,
Arivu


#6

what is the option for it… Ctrl S??


#7

use Send hot Key activity

Alt + h then d and then ctrl + S

Regards,
Arivu


#8

:+1:


#9

Alt F+S helps in saving the file


#10

or ALT 1… helps in saving an excel file


#11

Great then use the key to save the file.

Regards,
Arivu


#12

Just in case anyone else comes upon this question, an easier way to do this is to just use the invoke code activity and use VB.NET to remove it.

You’ll need to add the Microsoft.Office.Interop.Excel reference if you don’t already have it.

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
TryCast(wb.Sheets(sheetname), Microsoft.Office.Interop.Excel.Worksheet).Delete()
xlApp.DisplayAlerts = True

wb.Save()
wb.Close()

This uses 2 string input arguments. filepath is the full file path of the workbook, including the extension. sheetname is the name of the worksheet you’d like to delete. This is the visible name of the worksheet NOT the VBA worksheet name defined in the VBA editing window.

I commonly need to delete just a single sheet, and just include it in a uipath loop if I need to remove more than one sheet. However, it’d be easy enough to modify the code yourself to loop through an array of strings that’s passed in for sheetname, or to loop through based on sheet index. If you do go the index route, make sure you delete from last to first because the index of later items will change if you start deleting from the beginning.


Delete a sheet in Excel