Compare value

smartguy

Well-known Member
Joined
Jul 14, 2009
Messages
778
Hello all,

I have excel file in the below format.

Sheet1:

Excel Workbook
AB
1Datavalue
2451245617
3462547563
4481234
5451545454545
Sheet1


Sheet 2:

Excel Workbook
AB
1Datavalue
2451245617
3481234
4451545454545
Sheet2


I want to compare data based on "B" column value Sheet1 to Sheet2.

i want missing data in to sheet3.

Answer :

Excel Workbook
AB
1Datavalue
2462547563
Sheet3



Please help to provide VBA MAcro...
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi, Smartguy , Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG26May48
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] shts [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] sNum [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
 shts = Array("Sheet1", "Sheet2")
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
            .CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] sNum = 0 To UBound(shts)
 [COLOR="Navy"]With[/COLOR] Sheets(shts(sNum))
       [COLOR="Navy"]Set[/COLOR] Rng = .Range(.Range("B2"), .Range("B" & Rows.Count).End(xlUp))
   [COLOR="Navy"]End[/COLOR] With
        
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value, Dn.Offset(, -1)
    [COLOR="Navy"]Else[/COLOR]
         .Remove Dn.Value
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]Next[/COLOR] sNum
Sheets("Sheet3").Range("A2").Resize(.Count, 2) = Application.Transpose(Array(.items, .Keys))
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mickg !!!

Thanks for your Reply.

Some times additional data also come in Sheet2.

But that data no need to compare.


Excel Workbook
AB
1Datavalue
2451245617
3481234
4451545454545
51012345
Sheet2


Wee need to check whether sheet 1 data is there in sheet2 . if not there what the data. that it.

i think you will clear now...
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG26May31
[COLOR="Navy"]Dim[/COLOR] Rng1 [COLOR="Navy"]As[/COLOR] Range, Dn1 [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Rng2 [COLOR="Navy"]As[/COLOR] Range, Dn2 [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Fd [COLOR="Navy"]As[/COLOR] Boolean
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]With[/COLOR] Sheets("Sheet1")
    [COLOR="Navy"]Set[/COLOR] Rng1 = .Range(.Range("B2"), .Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
    [COLOR="Navy"]Set[/COLOR] Rng2 = .Range(.Range("B2"), .Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
    ReDim Ray(1 To Rng1.Count + Rng2.Count, 1 To 2)
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn1 [COLOR="Navy"]In[/COLOR] Rng1
        Fd = False
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn2 [COLOR="Navy"]In[/COLOR] Rng2
            [COLOR="Navy"]If[/COLOR] Dn1 = Dn2 [COLOR="Navy"]Then[/COLOR] Fd = True
        [COLOR="Navy"]Next[/COLOR] Dn2
            [COLOR="Navy"]If[/COLOR] Fd = False [COLOR="Navy"]Then[/COLOR]
                c = c + 1
                Ray(c, 1) = Dn1.Offset(, -1)
                Ray(c, 2) = Dn1
            [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Dn1
Sheets("Sheet3").Range("A2").Resize(c, 2) = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks Mr.MickG

Your Rocking ......

it's Working Fine....

969104r4vq237ph4.gif
 
Upvote 0
Hi Mickg,

Sheet 1 to Sheet 2 Working fine
Sheet 2 to Sheet 1 i got error

If Sheet 2 New data found it's working fine. New data not found i got error.

Application Defined Object error.

Now i compare Sheet 2 to Sheet1.
Please help
 
Upvote 0
Try this:-
Sheet 2 to sheet 1.
Results sheet3, column "D"
Code:
[COLOR="Navy"]Sub[/COLOR] MG26May23
[COLOR="Navy"]Dim[/COLOR] Rng1 [COLOR="Navy"]As[/COLOR] Range, Dn1 [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Rng2 [COLOR="Navy"]As[/COLOR] Range, Dn2 [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Fd [COLOR="Navy"]As[/COLOR] Boolean
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]With[/COLOR] Sheets("Sheet1")
    [COLOR="Navy"]Set[/COLOR] Rng1 = .Range(.Range("B2"), .Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
    [COLOR="Navy"]Set[/COLOR] Rng2 = .Range(.Range("B2"), .Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
    ReDim Ray(1 To Rng1.Count + Rng2.Count, 1 To 2)
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn2 [COLOR="Navy"]In[/COLOR] Rng2
        Fd = False
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn1 [COLOR="Navy"]In[/COLOR] Rng1
            [COLOR="Navy"]If[/COLOR] Dn1 = Dn2 [COLOR="Navy"]Then[/COLOR] Fd = True
        [COLOR="Navy"]Next[/COLOR] Dn1
            [COLOR="Navy"]If[/COLOR] Fd = False [COLOR="Navy"]Then[/COLOR]
                c = c + 1
                Ray(c, 1) = Dn2.Offset(, -1)
                Ray(c, 2) = Dn2
            [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Dn2
Sheets("Sheet3").Range("D2").Resize(c, 2) = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi MickG,

I got an error.

If Sheet1 & Sheet2 Same Value Means i got an error.

Application Defined object error
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

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