Compare 2 range and rearrange data

smanith

New Member
Joined
Apr 15, 2019
Messages
3
Hi all,
am new to excel macro VB, I have a requirement , where i have the below data:
NumberValueNumberValueDifference
NumberValueNumberValue
11399924758020161717.6
115000161717.61139021611615
11914021780.241139992475284
119150274086.811914021780.24
119160792537.1119150274086.8
119180349912.7119160792537.9
119190308752.5119180320793.7
11921080364119190308752.5
119220298546.511921080364
119250791109.7119220298546.5
119280704297.7119250791109.7
119330239020119280704297.7
180014306944.2119330239020

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>
</tbody>

Here 2 sets are there, i need to compare column1(Number) with column3(Number) and rearrange the whole table with matching Number values. For example i need the result as below:

NumberValueNumberValue
11399924758021139992475284
115000161717.6
11914021780.2411914021780.24
119150274086.8119150274086.8
119160792537.1119160792537.9
119180349912.7119180320793.7
119190308752.5119190308752.5
1192108036411921080364
119220298546.5119220298546.5
119250791109.7119250791109.7
119280704297.7119280704297.7
119330239020119330239020
180014306944.2
0161717.6
1139021611615


<colgroup><col><col span="3"></colgroup><tbody>
</tbody>

Could you please help me out with this? Read in the forum that it can be achieved using dictionary. But since am new here, could not proceed with it.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this:-
NB:- This code will alter your data!!

Code:
[COLOR="Navy"]Sub[/COLOR] MG15Apr13
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
Ray = Rng.Offset(, 2).Resize(, 2).Value
MsgBox Ray(1, 2)
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
    [COLOR="Navy"]For[/COLOR] n = 1 To UBound(Ray, 1)
        .Item(Ray(n, 1)) = Ray(n, 2)
    [COLOR="Navy"]Next[/COLOR] n
    Rng.Offset(, 2).Resize(, 2).ClearContents

    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
            Dn.Offset(, 2) = Dn.Value
            Dn.Offset(, 3).Value = .Item(Dn.Value)
            .Remove (Dn.Value)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR]

Range("C" & UBound(Ray, 1) + 2).Resize(.Count, 2) = Application.Transpose(Array(.Keys, .items))
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick,
Thanks you for the quick response!
I tried running your code for the below data:

1139993131202,251139021545898,66
11914048001139993131202,13
119150333861,681191404800
119160536733,24119150333861,68
119180345958,89119160536733,24
119190383234,08119180345958,89
11921077880119190383234,08
119220292658,411921077880
119250881794,83119220292658,4
119280830957,84119250881794,83
11931542519,1119280
830957,84
11933023560011931516000
18001891840119330235600

<tbody>
</tbody>


Result i got:

1139993131202,251139993131202,131
11914048001191404800
119150333861,68119150333861,68
119160536733,24119160536733,24
119180345958,89119180345958,89
119190383234,08119190383234,08
1192107788011921077880
119220292658,4119220292658,4
119250881794,83119250881794,83
119280
830957,84
113902
1545898,658
11931542519,111931516000
119330235600119330235600
18001891840

<tbody>
</tbody>

non matching numbers are in same row -highlighted in red
one of the record from second set is missing in result - highlighted in green

Could you please help?
 
Upvote 0
I could not duplicate that , but I have changed the last row variable, see if that solves the problem!!!
Code:
[COLOR="Navy"]Sub[/COLOR] MG16Apr22
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
Ray = Rng.Offset(, 2).Resize(, 2).Value
Lst = Range("A" & Rows.Count).End(xlUp).Row
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
    [COLOR="Navy"]For[/COLOR] n = 1 To UBound(Ray, 1)
        .Item(Ray(n, 1)) = Ray(n, 2)
    [COLOR="Navy"]Next[/COLOR] n
    Rng.Offset(, 2).Resize(, 2).ClearContents

    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
            Dn.Offset(, 2) = Dn.Value
            Dn.Offset(, 3).Value = .Item(Dn.Value)
            .Remove (Dn.Value)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR]

Range("C" & Lst + 1).Resize(.Count, 2) = Application.Transpose(Array(.Keys, .items))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick,
How would i change the code if i was comparing between two list that had 2 additional column each. A-C (A is the number) and D-F (D is the number) so that when you rearrange the list the set of column moves too.
Thanks
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG26Apr57
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
Ray = Rng.Offset(, 3).Resize(, 3).Value
Lst = Range("A" & Rows.Count).End(xlUp).Row
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
    [COLOR="Navy"]For[/COLOR] n = 1 To UBound(Ray, 1)
        .Item(Ray(n, 1)) = Array(Ray(n, 2), Ray(n, 3))
    [COLOR="Navy"]Next[/COLOR] n
    Rng.Offset(, 3).Resize(, 3).ClearContents

    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
            Dn.Offset(, 3) = Dn.Value
            Dn.Offset(, 4).Value = .Item(Dn.Value)(0)
            Dn.Offset(, 5).Value = .Item(Dn.Value)(1)
            .Remove (Dn.Value)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant
c = Lst
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    c = c + 1
    Cells(c, "D") = K
    Cells(c, "E") = .Item(K)(0)
    Cells(c, "F") = .Item(K)(1)
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,941
Members
449,480
Latest member
yesitisasport

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