Arrange data in Horizontal.

avisoft20

Board Regular
Joined
Sep 10, 2016
Messages
63
Hello Sir,

I have a database ,

For Example table given below :

RowStateCityPin
1DelhiDelhi110092
2PunjabPunjab110097
3PunjabChandigarh110102
4Uttar PradeshLucknow110107
5Uttar PradeshKanpur110112
6Uttar PradeshAgra110117
7Uttar PradeshMeruth110117
8Uttar PradeshMatrhura110127
9Uttar PradeshMathura-1110127

<tbody>
</tbody>


















Ans answer will ,

StatePinCityCity-1
Delhi110092Delhi
Punjab110097Punjab
Punjab110102Chandigarh
Uttar Pradesh110107Lucknow
Uttar Pradesh110112Kanpur
Uttar Pradesh110117AgraMeruth
Uttar Pradesh110127MatrhuraMeruth-1

<tbody>
</tbody>














In rows no 6 & 7 City-Agra and Meruth have same pin code and rows no 8 & 9 city - Mathura and Mathura-1 have same pin code.I want that if same pin code with same state found city will be in next column in separate cell.
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this:-
Your data assumed to start "A1" with the Word "State".(ex column 2 from your Thread Data)
Results start "E1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG15Oct41
[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] txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Q [COLOR="Navy"]As[/COLOR] Variant, oMax [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
ReDim Ray(1 To Rng.Count, 1 To 3)
[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
    txt = Dn.Value & Dn.Offset(, 2).Value
    [COLOR="Navy"]If[/COLOR] Not Dic.Exists(txt) [COLOR="Navy"]Then[/COLOR]
        n = n + 1
        Ray(n, 1) = Dn.Value
        Ray(n, 2) = Dn.Offset(, 2).Value
        Ray(n, 3) = Dn.Offset(, 1).Value
        Dic.Add txt, Array(n, 3)
    [COLOR="Navy"]Else[/COLOR]
        Q = Dic(txt)
            Q(1) = Q(1) + 1
            [COLOR="Navy"]If[/COLOR] Q(1) > UBound(Ray, 2) [COLOR="Navy"]Then[/COLOR] ReDim Preserve Ray(1 To Rng.Count, 1 To Q(1))
            Ray(Q(0), Q(1)) = Dn.Offset(, 1).Value
            Ray(1, Q(1)) = "City-" & Q(1) - 3
            oMax = Application.Max(oMax, Q(1))
        Dic(txt) = Q
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]With[/COLOR] Range("E1").Resize(Dic.Count, oMax)
    .Value = Ray
    .Borders.Weight = 2
    .Columns.AutoFit
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,215,578
Messages
6,125,642
Members
449,245
Latest member
PatrickL

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