Compare two worksheets and populate the source tab name of each row that does not match

prab1101

New Member
Joined
Dec 5, 2019
Messages
2
Office Version
  1. 365
Hi there,

I got the following piece of code online that compares two tabs and populates the non-matching rows into a third sheet. However, I am also looking to populate the tab name on each row (in the first column), indicating where it came from on the third tab (non-matches). Would anyone be able to help?


VBA Code:
Option Explicit

Sub CompareIt()
    Dim ar As Variant
    Dim arr As Variant
    Dim Var As Variant
    Dim v()
    Dim i As Long
    Dim n As Long
    Dim j As Long
    Dim str As String
   
    ar = Sheet1.Cells(10, 1).CurrentRegion.Value
    With CreateObject("Scripting.Dictionary")
        .CompareMode = 1
        ReDim v(1 To UBound(ar, 2))
        For i = 2 To UBound(ar, 1)
            For n = 1 To UBound(ar, 2)
                str = str & Chr(2) & ar(i, n)
                v(n) = ar(i, n)
            Next
            .Item(str) = v: str = ""
        Next
        ar = Sheet2.Cells(10, 1).CurrentRegion.Resize(, UBound(v)).Value
        For i = 2 To UBound(ar, 1)
            For n = 1 To UBound(ar, 2)
                str = str & Chr(2) & ar(i, n)
                v(n) = ar(i, n)
            Next
            If .exists(str) Then
                .Item(str) = Empty
            Else
                .Item(str) = v
            End If
            str = ""
        Next
        For Each arr In .keys
            If IsEmpty(.Item(arr)) Then .Remove arr
        Next
        Var = .items: j = .Count
    End With
    With Sheet3.Range("a1").Resize(, UBound(ar, 2))
        .CurrentRegion.ClearContents
        .Value = ar
        If j > 0 Then
            .Offset(1).Resize(j).Value = Application.Transpose(Application.Transpose(Var))
        End If
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi @prab1101, welcome to the board!

Try this

VBA Code:
Sub CompareIt()
    Dim ar As Variant, arr As Variant, Var As Variant, v() As Variant
    Dim i As Long, n As Long, m As Long, j As Long
    Dim str As String
 
    ar = Sheet1.Cells(10, 1).CurrentRegion.Value
    With CreateObject("Scripting.Dictionary")
        .CompareMode = 1
        ReDim v(1 To UBound(ar, 2) + 1)
        For i = 2 To UBound(ar, 1)
            m = 1
            v(m) = Sheet1.Name
            For n = 1 To UBound(ar, 2)
                str = str & Chr(2) & ar(i, n)
                m = m + 1
                v(m) = ar(i, n)
            Next
            .Item(str) = v
            str = ""
        Next
       
        ar = Sheet2.Cells(10, 1).CurrentRegion.Resize(, UBound(v) - 1).Value
        For i = 2 To UBound(ar, 1)
            m = 1
            v(m) = Sheet2.Name
            For n = 1 To UBound(ar, 2)
                str = str & Chr(2) & ar(i, n)
                m = m + 1
                v(m) = ar(i, n)
            Next
            If .exists(str) Then
                .Item(str) = Empty
            Else
                .Item(str) = v
            End If
            str = ""
        Next
        For Each arr In .keys
            If IsEmpty(.Item(arr)) Then .Remove arr
        Next
        Var = .items
        j = .Count
    End With
   
    With Sheet3.Range("a1").Resize(, UBound(ar, 2) + 1)
        .CurrentRegion.ClearContents
        .Cells(1, 1).Value = "Sheet"
        .Offset(, 1).Resize(1, UBound(ar, 2)).Value = ar
        If j > 0 Then
            .Offset(1).Resize(j).Value = Application.Transpose(Application.Transpose(Var))
        End If
    End With
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,660
Messages
6,120,787
Members
448,994
Latest member
rohitsomani

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