Filter and Delete Rows in Excel Files
The example below explains how to delete unwanted rows from an .xlsx file and how to filter the information by applying specific criteria. It presents activities such as Remove Data Row, Read Range Workbook or Write Range. You can find these activities in the UiPath.Excel.Activities package.
The .xlsx file used in the example includes the following columns: Name, Km, Price, and SellerType. The automation process deletes all the rows that contain the value Other and Private Seller from the SellerType column and creates a new sheet with the filtered results.
This is how the automation process can be built:
- Open Studio and create a new Process named by default Main.
Make sure to add the .xlsx file that you want to use in the project folder. You can also use the file we provided by downloading the example available at the bottom of this page.
-
Drag a Flowchart in the Workflow Designer.
- Create the following variables:
Variable Name Variable Type Default Value carsListWorkbookApplication N/A inputListSystem.Data.DataTable N/A -
Drag an Excel Application Scope activity in the Flowchart and connect it to the Start Node.
- Double click on the Excel Application Scope activity to open it.
- In the Properties panel, add the name
Delete Datain the DisplayName field. - Add the path of the
.xlsxfile in the WorkbookPath field, in this case,Cars.xlsx. - Select the check boxes for the AutoSave, CreateNewFile and Visible options. The robot is now allowed to create a new Excel workbook, to automatically save all the changes made to it and to open the Excel file in the foreground while performing actions on it.
- Add the variable
carsListin the Workbook field.
-
Select the Do sequence inside the Excel Application Scope activity and create the following variable:
Variable Name Variable Type Default Value filterListSystem.String[] {"Private Seller", "Other") -
Drag an Excel Read Range activity inside the Do sequence.
- In the Properties panel, add the name
"Cars"in the SheetName field. - Select the AddHeaders check box for considering the first row of the table a header.
- Add the variable
inputListin the DataTable field.
- In the Properties panel, add the name
-
Place a For Each activity below the Read Range activity.
- Add the value
wordin the ForEach field. - In the Properties panel, select the option
Stringfrom the TypeArgument drop-down list and add the variablefilterListin the Values field.
- Add the value
-
Select the Body container inside the For Each activity and create the following variables:
Variable Name Variable Type Default Value queryString RowsToDeleteSystem.Data.DataRow[] deletedRowsInt32 -
Drag an Assign activity inside the Do container.
- Add the variable
queryin the To field and the expressionstring.Format("[SellerType]='{0}'", word)in the Value field. This returns the matching values in the SellerType column and stores them in thequeryvariable. Thestring.Formatmethod is used to convert the DataRow objects that match the filter words to strings.
- Add the variable
-
Drag another Assign activity and place it below the first one.
- Add the variable
RowsToDeletein the To field and the expressioninputList.Select(query)in the Value field.
- Add the variable
-
Add a third Assign activity under the first two Assign activities.
- Add the variable
deletedRowsin the To field and the expressionRowsToDelete.Count-1in the Value field. This creates a counter for the rows that needs to be deleted.
- Add the variable
-
Place a While activity under the Assign activities.
- Insert the expression
deletedRows>=0in the Condition field. This means that the activities in the Body section are performed until the value of the variabledeletedRowsequals0.
- Insert the expression
-
Drag a Remove Data Row activity to the Body section of the While activity.
- Add the variable
inputListin the DataTable field. - Add the expression
RowsToDelete(deletedRows)in the Row field.
- Add the variable
-
Place an Assign activity below the Remove Data Row activity.
- Add the variable
deletedRowsin the To field and the expressiondeletedRows-1in the Value field. This creates a loop that is executed until the value of the variabledeletedRowsequals0.
- Add the variable
-
Return to the Flowchart workflow.
-
Drag an Excel Application Scope activity and connect it to the previous Delete Data Excel Application Scope activity.
- Double click on the Excel Application Scope activity to open it.
- In the Properties panel, add the name
Filter Datain the DisplayName field. - Select the check boxes for the AutoSave and CreateNewFile options. The robot is now allowed to create a new Excel workbook and to automatically save all the changes made to it.
- Add the variable
carsListin the Workbook field.
-
Drag a Close Application activity inside the Do container of the Excel Application Scope activity.
- Open the
.xlsxfile that you are using for this automation. For this example isCars.xlsx. - Click on the Options Menu button, select the Indicate on screen option and indicate the
.xlsxfile.
- Open the
-
Drag an Excel Write Range activity below the Close Application activity.
- In the Properties panel, add the name
"Cars2"in the SheetName field. - Add the variable
inputListin the DataTable field and the path"Cars.xlsx"in the WorkbookPath field. - Select the AddHeaders check box for considering the first row of the table a header.
- In the Properties panel, add the name
-
Run the process. The automation process deletes the specified data and filters the information in a new workbook sheet.