Create a Unique ID in a column in Excel

Hi Folks,

I have a scenario in UiPath, wherein I have a column named ‘ID’ in a DT. It has Unique ID’s uptil 745, as a number.

Now, I want to add more rows in the same DT. When new rows are added it should allott a new ID number , and it should be unique and not match with previous existing ID.

Please help me on this solution :slight_smile:

Hi @shikharno.7

Try this Steps

  1. Read Existing Data into dtExisting (use Read Range or populate it in some way)
  2. Initialize highestID as 0
  3. For Each row In dtExisting
    If Convert.ToInt32(row(“ID”)) > highestID Then
    highestID = Convert.ToInt32(row(“ID”))
  4. Create dtNew (Build Data Table with the same structure as dtExisting)
  5. For Each newDataRow In newRowsToAdd
    highestID = highestID + 1
    newRow = dtNew.NewRow()
    newRow(“ID”) = highestID
    ’ Set other columns in newRow as needed
    dtNew.Rows.Add(newRow)
  6. Merge dtNew into dtExisting (if needed)
1 Like

Hello @shikharno.7

Read existing data into DataTable
DataTable dt = ReadDataFromSource();

Find the maximum existing ID
int maxExistingID = dt.AsEnumerable().Max(row => Convert.ToInt32(row[“ID”]));

Generate and add new rows with unique IDs
for each newDataRow in NewDataToAdd
{
maxExistingID++; // Increment the maximum ID
DataRow newRow = dt.NewRow();
newRow[“ID”] = maxExistingID;
Set other values for the new row
Add the newRow to the DataTable
dt.Rows.Add(newRow);
}

Thanks & Cheers!!!

Hi

Here you can use Random method to generate random unique numbers

  1. Initialize Variable: newRandomNumber = 0
  2. Do While: condition is (YourDataTable.AsEnumerable().Any(Function(row) row(“UniqueID”).ToString() = newRandomNumber.ToString()))

3 inside the loop use a
Assign activity

newRandomNumber = New Random().Next(1, 1000)

Adjust the range as needed.

  1. Add a new row to YourDataTable with assign like this
    a. Assign: YourDataTable.Rows(YourDataTable.Rows.Count - 1)(“UniqueID”) = newRandomNumber

I can use add Datarow also here where pass the newRandomNumber as input in ArrayRow

Hope this helps

Cheers @shikharno.7

1 Like

Hi @Palaniyappan ,

I am trying to work out the logic. Getting below error, while assigning statement (Assign: YourDataTable.Rows(YourDataTable.Rows.Count - 1)(“UniqueID”) = newRandomNumber)

image

I have uploaded the FIle and Xaml , can you please have a look, where am I missing here, please?

Main.xaml (9.7 KB)
Master_File_new - Copy.xlsx (10.0 KB)

in such case we could configure the ID column as an autoincrement column and it would increase the next ID automatically

@ppr , but in such case , there might be a possibility of a duplicate ?
Will need to avoid that as well.

Sample Excel
Master_File_new - Copy.xlsx (10.0 KB)

we already got this from the begin on.

About the autoincrement you can refer to the docu

We can also crosscheck on Duplicates / next number e.g. with a LINQ

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.