Delete column values

I want to delete some random columns from a datatable by keeping the header of the column so thatt I can write those in Excel file. How to proceed with that

If you keep the header you arenā€™t deleting the column. Do you mean you want to set all the values to blank?

For Each Row in Datatable

  • Assign CurrentRow(ā€œcolumnNameā€) = ā€œā€
1 Like

Hi @neha_shisodiya

To delete specific columns from a DataTable in UiPath while keeping the column headers so that you can later write the modified DataTable to an Excel file, you can use the following approach:

  1. Retrieve the List of Columns to Delete:

    • Create a list (e.g., columnsToDelete) that contains the names of the columns you want to delete. Populate this list with the names of the columns you want to remove.
  2. Filter the Columns to Keep:

    • Use LINQ to create a new DataTable that contains only the columns you want to keep. You can use the CopyToDataTable method to achieve this.
  3. Write the Modified DataTable to Excel:

    • Write the modified DataTable (with the desired columns removed) to an Excel file using the ā€œWrite Rangeā€ activity.

Hereā€™s an example workflow in UiPath:

1. Assign activity:
   - Create a List(Of String) variable named columnsToDelete and populate it with the names of columns you want to delete.

2. Assign activity:
   - Create a DataTable variable (e.g., dtModified) and set it to the result of a LINQ query that filters the columns to keep.
   - Example LINQ query:
     dtOriginal.AsEnumerable().Select(Function(row) row.Field(Of String)("Column1")).CopyToDataTable()

3. Write Range activity:
   - Use the "Write Range" activity to write the dtModified DataTable to an Excel file.

4. End of Workflow

Replace ā€œColumn1ā€ with the actual column names in your DataTable, and repeat the LINQ query for each column you want to keep.

This workflow will create a new DataTable (dtModified) that only contains the columns you want to keep, and then it writes this modified DataTable to an Excel file while preserving the column headers.

Thanks!!

1 Like

They donā€™t want to delete the columns.

@postwick

This is what she requested, she is looking to delete the random columns from a datatable.

ā€œI want to delete some random columns from a datatableā€

Keep reading: "ā€¦ by keeping the header of the column so thatt I can write those in Excel file. "

I read the full question already and provide the solution accordingly.

Why donā€™t you let her decide which solution fit for her query. If the user will have any query they can directly ask me and also I didnā€™t asked for the feedback on my solution.

I want to delete the values not the rows and again I want to write that datatable to Excel file.

Sorry but not following your solution as well

Basically my use case is I want to read an Excel file and delete values from 3 different columns keeping the column name intact.

Then you use For Each Row in Datatable to loop through the datatable, and use Assign to set each column to blank.

The columns are not consecutive columns so at a time I can read only one column. Also it is a huge file. So canā€™t read the full Excel.

It doesnā€™t matter if theyā€™re consecutive columns.

You use Workbook Read Range to read the whole file, then For Each Row in Datatable to loop through it and set the columns you want (referenced by column name) to set them to blank values. Then you can write back to Excel.

I canā€™t read the whole file it is taking soo much time . Thatā€™s why Iā€™m reading one column at a time in the Excel scope.

So Read Range that column, For Each Row in Data Table and Assign to blank out the values, then Write Range back to the Excel file to overwrite the column with the blank values.

1 Like

Hello @neha_shisodiya

Assuming you have a DataTable named ā€˜inputDataTableā€™ and a List of column names to delete named ā€˜columnsToDeleteā€™

Remove the specified columns
columnsToDelete.ForEach(Function(column) inputDataTable.Columns.Remove(column))

Write the modified ā€˜inputDataTableā€™ to an Excel file

WriteRange(activity: WorkbookPath = ā€œC:\Path\To\Your\Excel\File.xlsxā€, SheetName = ā€œSheet1ā€, DataTable = inputDataTable, Range = ā€œA1ā€)

Thanks & Cheers!!!