Comparing column name between 2 excel

Hi Guys,

i need to compare between column name in 2 excel and copy all row in that column name.
how do i do it?

Hi @zaffan.isa

Can you share the sample Input and expected output.

Regards

Sure

A spreadsheet displays three columns labeled ID, Name, and Age, with corresponding values of 1, 2, and 3 for ID; John, Lemon, and Same for Name; and 34, 35, and 36 for Age. (Captioned by AI)
Sample Input

image
Expected Output

Both are different excel file and they have different format of data table

Hi @zaffan.isa

Can you please share 2nd excel file

Regards,

This is the second excel.

image

@zaffan.isa

Can you try below query

FinalDT = DT1.DefaultView.ToTable(False, DT1.Columns.Cast(Of DataColumn)() _
    .Where(Function(col) DT2.Columns.Contains(col.ColumnName)) _
    .Select(Function(col) col.ColumnName).ToArray())

Regards,

@zaffan.isa

Another approach

Invoke Code:

resultDT = DT2.Clone()

For Each row As DataRow In DT1.Rows
    Dim newRow As DataRow = resultDT.NewRow()
    For Each col As DataColumn In resultDT.Columns
        If DT1.Columns.Contains(col.ColumnName) Then
            newRow(col.ColumnName) = row(col.ColumnName)
        End If
    Next
    resultDT.Rows.Add(newRow)
Next

Input:

DT1

The image is a screenshot of an Excel spreadsheet with three columns labeled "ID," "Name," and "Age," containing three rows of data with IDs 1, 2, and 3, names John, Lemon, and Same, and ages 34, 35, and 36 respectively. (Captioned by AI)

DT2

The image shows a blank Excel spreadsheet with columns labeled "Name," "Address," and "Age." (Captioned by AI)

Output:

The image shows a spreadsheet with three columns labeled "Name," "Address," and "Age," where the names "John," "Lemon," and "Same" have corresponding ages of 34, 35, and 36, but the "Address" column is empty. (Captioned by AI)

Regards,

i have run the invoke code. The data remove the header. All of the data is written starting at first row.

Hello @zaffan.isa ,

For what I understood you just want to insert all the rows from excel 1 on excel 2, based on the column name.

You can find a Xaml attached with a solution. You can improve the solution with LINQ if you prefer.

If you have any doubt feel free to reach me.

Example.xaml (9.0 KB)

@zaffan.isa

Can you please share your xaml file

Regards,

i forgot to declare the DT, my apologies.

But now, i am having another problem is that the written data is overwrite my header.

I am sorry i cannot share the xaml. Because it contain confidential information

i will try. thanks for suggest

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