Macro to combine data under 3rd and 4th Column if condn satisfy for 1st , 2nd column

shansakhi

Active Member
Joined
Apr 5, 2008
Messages
276
Office Version
  1. 365
Platform
  1. Windows
Hello Everybody,
Good Morning!!
I am looking for a macro which will combine the data available in 3rd and 4th Column if conditions satisfied for 1st and 2nd Column.

<tbody>
</tbody>


Below Input and Output data for your reference..

INPUT
KMLegOrgDest
2,019,769,1841BOMDEL
2,019,769,1842DELBLR
2,019,770,0951BOMMAA
2,019,770,6752MAADEL
2,019,770,2131SXRIXJ
2,019,770,2132IXJDEL
2,019,770,2133DELBOM
2,019,770,2134BOMSXR
2,019,770,8162DELMAA
2,019,770,8163MAAIXC
OUTPUTRule
KMLegOrgDestOUTPUT
2,019,769,1841BOMDELBOM - DEL - BLRKM are same for Leg 1 & 2 hence Org and Dest combined in column E
2,019,769,1842DELBLR
2,019,770,0951BOMMAABOM - MAAKM is only one and not matching with any one hence Org and Dest taken as it is in column E
2,019,770,6752MAADELMAA - DELKM is only one and not matching with any one hence Org and Dest taken as it is in column E
2,019,770,2131SXRIXJSXR - IXJ - DEL - BOM - SXRKM are same for Leg 1, 2, 3 & 4 hence Org and Dest combined in column E
2,019,770,2132IXJDEL
2,019,770,2133DELBOM
2,019,770,2134BOMSXR
2,019,770,8162DELMAADEL - MAA - IXCKM are same for Leg 2 & 3 hence Org and Dest combined in column E
2,019,770,8163MAAIXC
2,019,770,8006KLHBOMKLH - BOMKM is only one and not matching with any one hence Org and Dest taken as it is in column E

<tbody>
</tbody>



Thank You,

Shan
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this:-
NB:- Numbers in column"A" assumed to start "A2".
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Dec06
[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] Dic [COLOR="Navy"]As[/COLOR] Object, Q [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
[COLOR="Navy"]If[/COLOR] Not Dic.Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
    Dic.Add Dn.Value, Array(Dn.Offset(, 2).Value & "-" & Dn.Offset(, 3).Value, Dn)
    Dn.Offset(, 4).Value = Dn.Offset(, 2).Value & "-" & Dn.Offset(, 3).Value
[COLOR="Navy"]Else[/COLOR]
Q = Dic(Dn.Value)
[COLOR="Navy"]For[/COLOR] n = 2 To 3
    [COLOR="Navy"]If[/COLOR] Not InStr(Q(0), Dn.Offset(, n).Value) > 0 [COLOR="Navy"]Then[/COLOR]
        Q(0) = Q(0) & "-" & Dn.Offset(, n).Value
        Q(1).Offset(, 4) = Q(0)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
Dic(Dn.Value) = Q
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]


[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thank you Sir for your reply its working fine but few challenges I am facing in code..

If I have same KM for two different passengers for same Org and Dest code is combining Org and Dest only for one.

KMLegOrgDest
2,019,770,8161DELMAADEL-MAA
2,019,770,8161DELMAA

<colgroup><col width="121" span="5" style="width:91pt"> </colgroup><tbody>
</tbody>

It should pickup as below

KMLegOrgDest
2,019,770,8161DELMAADEL-MAA
2,019,770,8161DELMAADEL-MAA

<colgroup><col width="121" span="5" style="width:91pt"> </colgroup><tbody>
</tbody>

I think code is not considering Leg no. it is considering only KM....

I need a code which will consider both KM and Leg ...


Thank you

Regards,
Shan
 
Upvote 0
What would the result of this look like:-

<colgroup><col><col span="3"></colgroup><tbody>
</tbody>
Code:
[COLOR="RoyalBlue"][B]Row No [/B][/COLOR] [COLOR="RoyalBlue"][B]Col(A)     [/B][/COLOR] [COLOR="RoyalBlue"][B]Col(B) [/B][/COLOR] [COLOR="RoyalBlue"][B]Col(C) [/B][/COLOR] [COLOR="RoyalBlue"][B]Col(D) [/B][/COLOR]
1.      KM          Leg     Org     Dest   
2.      2019770816  2       DEL     MAA    
3.      2019770816  3       MAA     IXC    
4.      2019770816  1       DEL     MAA    
5.      2019770816  1       DEL     MAA
Regards Mick
 
Upvote 0
DEL-MAA--IXC

DEL-MAA

DEL-MAA

The Leg No and KM will always be in Sorting order from Lowest to highest..
 
Upvote 0
KMLegOrgDest
2,019,770,8162DELMAADEL-MAA-IXC
2,019,770,8163MAAIXC
2,019,770,8161DELMAADEL-MAA
2,019,770,8161DELMAADEL-MAA

<colgroup><col width="121" span="5" style="width:91pt"> </colgroup><tbody>
</tbody>


The Leg No and KM will always be in Sorting order from Lowest to highest..
 
Upvote 0
Would this then be the result if Key 1 had come before Key 2, or something else. ????


KMLegOrgDest
2,019,770,8161DELMAADEL-MAA-IXC
2,019,770,8161DELMAADEL-MAA-IXC
2,019,770,8162DELMAA
2,019,770,8163MAAIXC

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,145
Members
449,363
Latest member
Yap999

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