Sort Data in Excel Files
The example below explains how an .xlsx table can be processed to display the data presented in all rows in ascending order, without any duplicates.
It presents activities such as Remove Data Row, Read Range or Sort Table. You can find these activities in the UiPath.Excel.Activities package.
The example below uses a table comprising five columns - id, first-name, last-name, email, and gender. The entries in the table are arranged in descending order. Some of the rows appear several times along the spreadsheet. Using an automation process, data is sorted in ascending order and all duplicates are removed.
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 existingColumnsWorkbookApplication 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
Sorting rows ascendingin the DisplayName field. - Add the path of the
.xlsxfile in the WorkbookPath field, in this case,Book.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
existingColumnsin the Workbook field.
-
Select the Do sequence inside the Excel Application Scope activity and create the following variable:
Variable Name Variable Type Default Value descendingOrderSystem.Data.DataTable N/A -
Drag an Excel Read Range activity inside the Do sequence.
- In the Properties panel, add the name
"Sheet1"in the SheetName field. - Select the AddHeaders check box for considering the first row of the table a header.
- Add the variable
descendingOrderin the DataTable field.
- In the Properties panel, add the name
-
Place a Sort Table activity under the Read Range activity.
- In the Properties panel, add the name
"id"in the ColumnName field. - Select
Ascendingfrom the Order list. - Add the name
"Sheet1"in the SheetName field. - Add the name
"Table1"in the TableName field.
- In the Properties panel, add the name
-
Return to the Flowchart workflow.
-
Drag an Excel Application Scope activity and connect it to the previous Excel Application Scope activity.
- Double click on the Excel Application Scope activity to open it.
- In the Properties panel, add the name
Deleting duplicatesin the DisplayName field. - 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
existingColumnsin the Workbook field.
-
Drag an Excel Read Range activity inside the Do sequence.
- In the Properties panel, add the name
"Sheet1"in the SheetName field. - Select the AddHeaders check box for considering the first row of the table a header.
- Add the variable
descendingOrderin the DataTable field.
- In the Properties panel, add the name
-
Place a While activity below the Read Range activity.
- Create the following variable:
Variable Name Variable Type Default Value rowIndexInt32 0 - Add the expression
rowIndex+1 < descendingOrder.Rows.Countin the Condition field. This expression process the entire table and enables you to perform the While loop until therow index +1is smaller than the total number of rows.
-
Add a Sequence container in the body of the While activity.
- Create the following variables:
Variable Name Variable Type Default Value rowSystem.Data.DataRow N/A rowNextSystem.Data.DataRow N/A -
Place an Assign activity inside the previously created Sequence container.
- Add the variable
rowin the To field and the expressiondescendingOrder.Rows(rowIndex)in the Value field. This enables you to assign the information contained in a row to the row variable according to its index.
- Add the variable
-
Drag another Assign activity and place it below the previous one.
- Add the variable
rowNextin the To field and the expressiondescendingOrder.Rows(rowIndex+1)in the Value field. This assigns the value of the following row to therowNextvariable, and thus helps you iterate through all the table.
- Add the variable
-
Add an If activity under the Assign activity.
- Add the expression
row(0).ToString=rowNext(0).Tostringin the Condition field. This condition checks if the data contained in therowvariable is identical to the one in therowNextvariable.
- Add the expression
-
Drag a Remove Data Row activity in the Then field of the If activity.
- Add the variable
descendingOrderin the DataTable field and the variablerowNextin the Row field.
- Add the variable
-
Place an Assign activity in the Else field of the If activity.
- Add the variable
rowIndexin the To field and the expressionrowIndex+1in the Value field.
- Add the variable
-
Add a Write Range activity below the While activity.
- Add the name
"Sheet2"in the SheetName field, the value"A1"in the StartingCell field and the variabledescendingOrderin the DataTable field. - Select the AddHeaders check box for considering the first row of the table a header.
- Add the name
-
Run the process. The automation process creates a new sheet that displays the data in ascending order without any duplicates.