Compare Numeric Values
The example below explains how to compare the numeric values from two different columns of an Excel file. It presents activities such as Read Range, Excel Application Scope, or Write Cell. You can find these activities in the UiPath.Excel.Activities package.
This example compares the values from columns A and B and writes the results in a new column C. In the C column, = means that the numbers are equal, A means that the first number is greater, and B means that the second number is greater.
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 .csv 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 RowIndexInt32 1 -
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 path of the
.xlsxfile in the WorkbookPath field, in this case,columns.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 dtColumnsSystem.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. - Add the variable
dtColumnsin the DataTable field.
- In the Properties panel, add the name
-
Add a For Each Row activity below the Read Range activity.
- In the Properties panel, add the variable
dtColumnsin the DataTable field.
- In the Properties panel, add the variable
-
Drag an If activity inside the Body sequence of the For Each Row activity.
- Add the expression
cint(row(0))=cint(row(1))in the Condition field. Thecintmethod converts the values extracted from the table to the Int32 format, enabling the comparison of the two numbers.
- Add the expression
-
Drag a Write Cell activity to the Then section of the If activity.
- In the Properties panel, add the expression
"C" + RowIndex.ToStringin the Range field and the variable"Sheet1"in the SheetName field. - Add the expression
"="in the Value field.
- In the Properties panel, add the expression
-
Drag an If activity to the Else section of the If activity.
- In the Properties field, add the name
If valuesin the DisplayName field. - Add the expression
cint(row(0)) > cint(row(1))in the Condition field.
- In the Properties field, add the name
-
Add a Write Cell activity in the Then field of the If values activity.
- In the Properties panel, add the expression
"C" + RowIndex.ToStringin the Range field and the variable"Sheet1"in the SheetName field. - Add the expression
Ain the Value field. This value is written in column C if the first number is greater than the second one.
- In the Properties panel, add the expression
-
Add a Write Cell activity in the Else field of the If values activity.
- In the Properties panel, add the expression
"C" + RowIndex.ToStringin the Range field and the variable"Sheet1"in the SheetName field. - Add the expression
Bin the Value field. This value is displayed in column C if the first number is smaller than the second one.
- In the Properties panel, add the expression
-
Place an Assign activity under the If activity, inside the body of the For Each Row activity.
- Add the variable
RowIndexin the To field and the expressionRowIndex+1in the Value field. The expressionRowIndex+1causes the row index to increase every time a loop is performed, until the end of the table is reached.
- Add the variable
-
Run the process. The automation process analyzes the data and creates a new column with the compared values.