How to compare two columns in excel to get the sub string from one column?



In excel I have to compare column “Company” with the column “Name” . If the company col contains any of the part of the string from Name column for that particular row, I have to delete the row. I am not able to figure out the if condition to be put for this check. Please provide me the some inputs on this. It’ll be great!



@Komal_Joshi, You can also use LINQ to select rows where value from “Company” Column dont contains value from “Name” column,

Datatable dtFinal = (From row in datatable.Select() Where not row("Company").ToString.Contains(row("Name").TOString) Select row).ToArray().CopyToDatatable()

Dominic :slight_smile:



Can u upload the excel sheet. I think your requirement is if the column company value contains a part of the Name value we have to select the row right.

Company, Name
abcd , hfg abc
fdgs , ghfb

now we have to select the first row right.
If Iam wrong please let me know.



Thank you. :slight_smile:


Yes. You are right. Further need to delete the entire row containing similar word.


ok. Thank you :slight_smile: Can you tell me how to extract row in excel and print row by row ? I want to print serial number from 1 to 100 in excel file under Sr. No column.


Try this
(From p in datatable.Select()
where Not p(“Name”).ToString.Split({" "},StringSplitOptions.RemoveEmptyEntries).Any(Function(d) p(“Company”).ToString.Contains(d))
Select p).ToArray.CopyToDataTable()



Hey Thank you @Dominic and @MAHESH1 your solutions worked fine but when I am using this condition on different excel, there is an error displayed as “Assign new dataTable to store valid rows : The source contains no DataRows.” I am unable to know how to resolve this now!
Can you kindly suggest any other way on it? your help is appreciated.



Your datatable might be empty. check once.



No actually it’s filled with data currently. The condition works fine in the case. For few excels it’s not working. I am not exactly aware of the cause.



I think your datatable is not having the values which satisfes the condition.
So if you try to convert empty array of datarows to datatable it will throw exception.

Before converting to datatable just check the array count>0 in if condition
If it is greater than zero then convert to datatable by using copy to datatable activity



OK. Thank you @MAHESH1 :slight_smile: