Delete a sheet in excel?

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

Hi @shrganes,

Please refer below post. @ddpadil

Regards,
Arivu

@shrganes, Refer this,
Delete Sheet

Regards,
Dominic :slight_smile:

thank you…

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

Hi @shrganes,

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

ALT+H+D+S

Regards,
Arivu

1 Like

what is the option for it… Ctrl S??

use Send hot Key activity

Alt + h then d and then ctrl + S

Regards,
Arivu

:+1:

Alt F+S helps in saving the file

or ALT 1… helps in saving an excel file

Great then use the key to save the file.

Regards,
Arivu

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.

8 Likes

@Dave,

I am trying your code to code to delete sheet from excel file and getting error like: Can’t find Interop type that matches embedded type ‘Microsoft.Office.Interop.Excel.Application’.

Where I need to add this reference. Could you please help me in this.

@lakshman

FYR

1 Like

@Vijay_Tulsalkar,

Thanks for this and will check.

Hi,
You may install a package -> AdvanceExcel. The package included an activity named “Delete Worksheet”.
Remember to mark the answer as solution.

Capture

5 Likes

Hi,

You can execute below mentioned VBA method to delete worksheet

Sub DeleteWorksheet(Sheetname As String)
Application.DisplayAlerts = False
Sheets(Sheetname).Delete
Application.DisplayAlerts = True
End Sub

Thanks,
Madhura

1 Like

hi @shrganes

Here is a detailed article on that :slight_smile:
# How To Delete Sheets From An Excel File Programmatically – In UiPath

Regards,

1 Like

Hi,

Try editing the xaml file with a text editor and add the following line under any assembly reference line in the file:

<AssemblyReference>Microsoft.Office.Interop.Excel</AssemblyReference>

1 Like