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.
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)