Delete a row from an Excel that has a certain value in a row

Hi, I have an Excel table that has multiple rows and columns.
I get a unique ID and I need to delete the row that has that ID (row name is “ID”).
How could I do that efficiently. Do I really need to go through all rows or is there a direct command?
->e.g. ID=123987

Thanks

1 Like

Hi @Jizh,

Just a suggestion, why don’t you try finding the element by sending hot keys ctrl + F, so that it can find the row you need

1 Like

You will have to open excel application scope
read the range “” in the correct sheet and create a datatable
do a for each row in the data table
run a if condition to check if any of the row contains ID= 123987
if condition is true remove data row else do nothing

i am just not able to write the condition in which we can run the if statement to check the id

give me a little time i am working on this

1 Like

try this. I am deleting ID 345 in the excel and getting the data into a fresh sheet.

RemoveDatarow.zip (8.4 KB)

credits : a old answer from @aksh1yadav

1 Like

I kind of agree that you really don’t want to loop through each row just to find the correct row. So the method to use so you don’t need to loop through each row is to filter your data down to an array of rows. You can do this using LINQ, and I apologize if this is too technically - using .Select() or preferrably .Where(Function(r) )

Another thing to know is that when you filter this data, you will get potentially multiple rows that match, so you need to run the new array through a For each to delete them. And, since they are an array of rows, the original data set is referenced by this array so they are deleted from it.

here is pseudocode that represents this idea:

Assign activity: arrRows = dt1.AsEnumerable.Where(Function(r) r("ID").ToString.Trim = "123987" ).ToArray

For each row in arrRows 'TypeArgument: DataRow'
   Delete Row activity: DataRow => row, DataTable => dt1

Regards.

3 Likes

Thanks a lot for the replies :slight_smile:
@ClaytonM I will try that solution.

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