How to copy specific columns from one DataTable to another?

How to copy specific columns from one DataTable to another?

I have a MS Excel sheet downloaded from a SAP server through a workflow which have 146 columns.

I need only 5 columns from those. I am able to create a DataTable through Excel Application scope and Read Range.

5 (+1, which I will create later) columns:

As per this comment,

I tried to populate the new Data Table:

var_columnNames = {"Name"}
dt_new = new DataView(dt_input).ToTable(false, var_columnNames)

But it seems three consecutive columns have the same name as Payment in and UiPath shows an error for duplicated Column Names. So I am trying with Add Headers as unchecked.

Can you suggest me if I am on the right track or a better approach?

If you just want to remove some columns, you can just delete from the DataTable variable and write back to excel using methods like DataTable.Columns.Remove(“columnName”) or by index DataTable.Columns.RemoveAt(columnIndex).

1 Like

Thanks @bcorrea . That would be tedious as I just want to keep 5 columns out of 146. Any other suggestions?

and i believe is not like the columns that you want to keep are next to each other right?

1 Like

Yes @bcorrea You are right. The columns that I want to keep aren’t next to each other.

ok, since the only way will be remove the columns you dont want, see attached solution that you can invoke passing the columns to keep as easy parameter: CleanDataTable.xaml (5.4 KB)


Hi @debanjan,


Please use Filter data table activity,

Example: GetSpecificColumns.xaml (6.9 KB) DataIn.xlsx (9.9 KB)



Thanks @bcorrea your shared workflow looks promising. So this VB code will remove the columns with respect to columnNames. Now before I ask, how to pass the columnNames which I want to keep, can we tweak the code block to keep the columns with respect to the column index e.g. column1, column5, column17, column3?

like this: {“Iwantthiscolumn”,“pleasekeepthisonetoo”,“i love this column”}


Thanks again @bcorrea I think this workflow will solve my issue but before applying it I have to ensure the DataTable gets populated properly (which was an initial barrier due to duplicate column name within the downloaded excel from SAP) . Please bear with me.

you sure will have error if you have duplicate excel columns, but you can write something to rename duplicates before reading it as datatable.

1 Like

Thanks @bcorrea I am out of words, all I can say "you are a gem ". Thanks again for your time and all the help.


Thanks @kadiravan_kalidoss I will take up your solution shortly and update you accordingly.

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