Activities
latest
false
Banner background image
Productivity Activities
Last updated Mar 27, 2024

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:

  1. Open Studio and create a new Process named by default Main.

    Note: 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.
  2. Drag a Flowchart in the Workflow Designer.

    • Create the following variables:

      Variable Name

      Variable Type

      Default Value

      existingColumns

      WorkbookApplication

      N/A

      RowIndex

      Int32

      1

  3. 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 .xlsx file 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 existingColumns in the Workbook field.
  4. Select the Do sequence inside the Excel Application Scope activity and create the following variable:

    Variable Name

    Variable Type

    Default Value

    dtColumns

    System.Data.DataTable

    N/A
  5. 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 dtColumns in the DataTable field.
  6. Add a For Each Row activity below the Read Range activity.

    • In the Properties panel, add the variable dtColumns in the DataTable field.
  7. 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. The cint method converts the values extracted from the table to the Int32 format, enabling the comparison of the two numbers.
  8. Drag a Write Cell activity to the Then section of the If activity.

    • In the Properties panel, add the expression "C" + RowIndex.ToString in the Range field and the variable "Sheet1" in the SheetName field.
    • Add the expression "=" in the Value field.
  9. Drag an If activity to the Else section of the If activity.

    • In the Properties field, add the name If values in the DisplayName field.
    • Add the expression cint(row(0)) > cint(row(1)) in the Condition field.
  10. Add a Write Cell activity in the Then field of the If values activity.

    • In the Properties panel, add the expression "C" + RowIndex.ToString in the Range field and the variable "Sheet1" in the SheetName field.
    • Add the expression A in the Value field. This value is written in column C if the first number is greater than the second one.
  11. Add a Write Cell activity in the Else field of the If values activity.

    • In the Properties panel, add the expression "C" + RowIndex.ToString in the Range field and the variable "Sheet1" in the SheetName field.
    • Add the expression B in the Value field. This value is displayed in column C if the first number is smaller than the second one.
  12. Place an Assign activity under the If activity, inside the body of the For Each Row activity.

    • Add the variable RowIndex in the To field and the expression RowIndex+1 in the Value field. The expression RowIndex+1 causes the row index to increase every time a loop is performed, until the end of the table is reached.
  13. Run the process. The automation process analyzes the data and creates a new column with the compared values.

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.