Unable to Read Range Workbook to a datatable

I have downloaded this data from a portal and it has two columns that have the same name that is ID. These 2 same IDs are needed hence I cannot deleted one but I can rename one. When I use the read range workbook and it gets there, I get the feedback that: Read Range Workbook: A column named ‘ID’ already belongs to this DataTable.

How do I tackle this?

@Baba_Karim_Abdulai

uncheck headers option in the properties then it would read

then you can replace names and set the headers again if you need

cheers

This is prone to happen as UiPath DataTable cannot contain duplicate column names.
To fix: Read without headers or Rename duplicate header.

Since you need both columns, the solution is to pre-rename the duplicate column header before loading into a DataTable.

Steps to do in UiPath.

  1. Read it without headers and save it to Datatable.
  2. Use invoke code and paste below code .
    Dim usedNames As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)

For i = 0 To dtRaw.Columns.Count - 1

Dim rawName As String = dtRaw.Rows(0)(i).ToString.Trim

' If blank → give a default name
If String.IsNullOrWhiteSpace(rawName) Then
    rawName = "Column_" & i.ToString
End If

Dim finalName As String = rawName
Dim counter As Integer = 1

' Ensure uniqueness
While usedNames.Contains(finalName)
    finalName = rawName & "_" & counter
    counter += 1
End While

' Rename the column
dtRaw.Columns(i).ColumnName = finalName
usedNames.Add(finalName)

Next

’ Remove header row
dtRaw.Rows.RemoveAt(0)

  1. Map argument in invoke code.

  1. Your new datatable is ready to use now with headers renamed automatically.


Sequence2.xaml (8.4 KB)

HI @Baba_Karim_Abdulai

You have two simple options:

  1. After reading with headers, rename the duplicate column directly:

dt.Columns(1).ColumnName = “ID_2”

  1. Read Range with addheaders unchecked, then manually assign unique names to both ID columns afterward.

If helpful, mark as solution. Happy automation with UiPath

Hi @Baba_Karim_Abdulai

If this design helped you fix the problem, please mark as solution so it can help others too :slight_smile: