compare two column according to the header

tommy8quko

New Member
Joined
Mar 27, 2017
Messages
18
Hi all,I got the below two sheet with the table look like below, and I would like to compare two table column to column (with the same header), and return true if exactly the same( like the below example),and return false if anything not the same (even the sequence is not the same ).
I am new to the vba, and wanna how to do it, many thanks!

Sheet 1,

[TABLE="width: 500"]
<tbody>[TR]
[TD]Food[/TD]
[TD]Price[/TD]
[TD]Amount[/TD]
[/TR]
[TR]
[TD]Apple[/TD]
[TD]$9[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]Banana[/TD]
[TD]$4[/TD]
[TD]200[/TD]
[/TR]
[TR]
[TD]Orange[/TD]
[TD]$5[/TD]
[TD]300[/TD]
[/TR]
</tbody>[/TABLE]

Sheet 2,

[TABLE="width: 500"]
<tbody>[TR]
[TD]Food[/TD]
[TD]Supplier[/TD]
[TD]Price[/TD]
[TD]Amount[/TD]
[/TR]
[TR]
[TD]Apple[/TD]
[TD]A[/TD]
[TD]$9[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]Banana[/TD]
[TD]B[/TD]
[TD]$4[/TD]
[TD]200[/TD]
[/TR]
[TR]
[TD]Orange[/TD]
[TD]C[/TD]
[TD]$5[/TD]
[TD]300
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
with this simple example this is not hard to do. What happens if one of your tables has multiple occurrences of say "apple"? How would the code know which instance goes with which instance?
 
Upvote 0
I build this with the sample data you provided.

This does not address the question that I asked in post #2

Code:
Sub tommy8quko()
Dim rng As Range, cell As Range, rngHEAD As Range, _
    rngHEAD2 As Range
Dim lngROW As Long, lngCOL As Long, lngROW2 As Long, _
    lngCOL2 As Long
Dim ws As Worksheet, ws2 As Worksheet
Dim intFOOD As Integer, intFOOD2 As Integer, _
    intPR As Integer, intPR2 As Integer, _
    intAMT As Integer, intAMT2 As Integer, _
    intCELL As Integer
Dim strCHK As String, strCHK2 As String
    
    Set ws = Sheets("Sheet 1")
    With ws
        lngROW = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
        lngCOL = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        Set rngHEAD = ws.Range(ws.Cells(1, 1), ws.Cells(1, lngCOL))
        intFOOD = rngHEAD.Find("Food").Column
        intPR = rngHEAD.Find("Price").Column
        intAMT = rngHEAD.Find("Amount").Column
        Set rng = ws.Range(ws.Cells(2, 1), ws.Cells(lngROW, 1))
        Set ws2 = Sheets("Sheet 2")
        With ws2
            lngROW2 = ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row
            lngCOL2 = ws2.Cells(1, ws2.Columns.Count).End(xlToLeft).Column
            Set rngHEAD2 = ws2.Range(ws2.Cells(1, 1), ws2.Cells(1, lngCOL2))
            intFOOD2 = rngHEAD2.Find("Food").Column
            intPR2 = rngHEAD2.Find("Price").Column
            intAMT2 = rngHEAD2.Find("Amount").Column
            Set rngHEAD2 = ws2.Range(ws2.Cells(2, 1), ws2.Cells(lngROW2, 1))
        End With
        For Each cell In rng
            strCHK = cell.Value & ws.Cells(cell.Row, intPR).Value _
                & ws.Cells(cell.Row, intAMT).Value
            intCELL = rngHEAD2.Find(cell.Value).Row
            strCHK2 = ws2.Cells(intCELL, intFOOD2).Value & ws2.Cells(intCELL, intPR2).Value _
                & ws2.Cells(intCELL, intAMT2).Value
            If strCHK <> strCHK2 Then
                ws.Range(ws.Cells(cell.Row, 1), _
                    ws.Cells(cell.Row, lngCOL)).Interior.Color = 252
                ws2.Range(ws2.Cells(intCELL, 1), _
                    ws2.Cells(intCELL, lngCOL2)).Interior.Color = 252
            End If
        Next cell
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top