Match cells in column, then transpose matching row data from column b, c etc on many

arcadian13

New Member
Joined
Jul 23, 2012
Messages
22
I have a spreadsheet with multiple tabs, and would like to transpose the data on each tab in the way described below:

So if I have:
1 a
1 b
1 c
2 a
2 c
2 d

I would like to get

1 a b c
2 a c d

Same with multicolumn. if I have

1 a b c
1 d
2 e f g h
2 i j

Would like to get

1 a b c d
2 e f g h i j

Am a coding dummy, so appreciate any help
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Place this in a basic Module and run from the activesheet.
Code:
[COLOR="Navy"]Sub[/COLOR] MG23Jul07
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Lst         [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] nRng        [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Q
[COLOR="Navy"]With[/COLOR] ActiveSheet
[COLOR="Navy"]Set[/COLOR] Rng = .Range(.Range("A1"), .Range("A" & Rows.count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
            Lst = Cells(Dn.Row, Columns.count).End(xlToLeft).Column
            [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
                .Add Dn.Value, Array(Dn, Lst)
            [COLOR="Navy"]Else[/COLOR]
                Q = .Item(Dn.Value)
                    Q(0).Offset(, Q(1)).Resize(, Lst).Value = Dn.Offset(, 1).Resize(, Lst).Value
                    Q(1) = Cells(Q(0).Row, Columns.count).End(xlToLeft).Column
                        [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
                            [COLOR="Navy"]Set[/COLOR] nRng = Dn
                        [COLOR="Navy"]Else[/COLOR]
                            [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dn)
                        [COLOR="Navy"]End[/COLOR] If
                .Item(Dn.Value) = Q
            [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]End[/COLOR] With
 [COLOR="Navy"]If[/COLOR] Not nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] nRng.EntireRow.Delete
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
That is like fire to a caveman - magic! Have tested, and it looks like it works. Can you change it to run on all the tabs in a given workbook in one go? At the moment, it runs on a tab at a time when I am in the tab, and then run the macro
 
Upvote 0
Try this:-
Code:
Sub Mult_Ws()
Dim Rng         As Range
Dim Dn          As Range
Dim Lst         As Integer
Dim nRng        As Range
Dim Q
Dim Ws As Worksheet
For Each Ws In ActiveWorkbook.Worksheets
With CreateObject("scripting.dictionary")
   .CompareMode = vbTextCompare
Set Rng = Ws.Range(Ws.Range("A1"), Ws.Range("A" & Rows.Count).End(xlUp))
For Each Dn In Rng
            Lst = Ws.Cells(Dn.Row, Columns.Count).End(xlToLeft).Column
            If Not .Exists(Dn.Value) Then
                .Add Dn.Value, Array(Dn, Lst)
            Else
                Q = .Item(Dn.Value)
                    Q(0).Offset(, Q(1)).Resize(, Lst).Value = Dn.Offset(, 1).Resize(, Lst).Value
                    Q(1) = Ws.Cells(Q(0).Row, Columns.Count).End(xlToLeft).Column
                        If nRng Is Nothing Then
                            Set nRng = Dn
                        Else
                            Set nRng = Union(nRng, Dn)
                        End If
                .Item(Dn.Value) = Q
            End If
Next
End With
 
 If Not nRng Is Nothing Then nRng.EntireRow.Delete
   Set nRng = Nothing
    Lst = 0
Next Ws
End Sub
 
Upvote 0
It would help me & others if you try to keep you requests within the thread'
Try this for you PM.
NB:- This code is also for Multi sheets.
Code:
[COLOR="Navy"]Sub[/COLOR] MG25Jul57
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Lst         [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] nRng        [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Q
[COLOR="Navy"]Dim[/COLOR] Ws [COLOR="Navy"]As[/COLOR] Worksheet
[COLOR="Navy"]Dim[/COLOR] Ac [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Ws [COLOR="Navy"]In[/COLOR] ActiveWorkbook.Worksheets
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
   .CompareMode = vbTextCompare
[COLOR="Navy"]Set[/COLOR] Rng = Ws.Range(Ws.Range("A1"), Ws.Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
   [COLOR="Navy"]Set[/COLOR] Ac = Ws.Range(Dn.Offset(, 1), Ws.Cells(Dn.Row, Columns.Count).End(xlToLeft))
    
    [COLOR="Navy"]If[/COLOR] Ac.Count = 1 [COLOR="Navy"]Then[/COLOR]
        Txt = Ac.Value
    [COLOR="Navy"]Else[/COLOR]
        Txt = "(" & Join(Application.Transpose(Application.Transpose(Ac.Value)), "_") & ")"
    [COLOR="Navy"]End[/COLOR] If
          Ac = vbNullString
          
            [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
                .Add Dn.Value, Array(Dn, Txt)
                 Dn.Offset(, 1) = Txt
            [COLOR="Navy"]Else[/COLOR]
                Q = .Item(Dn.Value)
                   Q(1) = Q(1) & "," & Txt
                     Q(0).Offset(, 1).Value = Q(1)
                        [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
                            [COLOR="Navy"]Set[/COLOR] nRng = Dn
                        [COLOR="Navy"]Else[/COLOR]
                            [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dn)
                        [COLOR="Navy"]End[/COLOR] If
                .Item(Dn.Value) = Q
            [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
.RemoveAll
[COLOR="Navy"]End[/COLOR] With
 
 [COLOR="Navy"]If[/COLOR] Not nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] nRng.EntireRow.Delete
   [COLOR="Navy"]Set[/COLOR] nRng = Nothing
    Lst = 0
[COLOR="Navy"]Next[/COLOR] Ws
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks for this. It gives me an error. Best way to see the problem is via a before and after attachment, but can't include those. One more thing: I realise now I don't want a tab (which will always be called "users") to be sorted.

After sorting two tabs in the right way, it gave an error for a tab , and then the other tabs were left unsorted


<colgroup><col width="64" span="13" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Try this for to not sort "Users".
If it still fails, you need to supply the data it failed on !!
Code:
[COLOR="Navy"]Sub[/COLOR] Mult_Ws()
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Lst         [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] nRng        [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Q
[COLOR="Navy"]Dim[/COLOR] Ws [COLOR="Navy"]As[/COLOR] Worksheet
[COLOR="Navy"]Dim[/COLOR] Ac [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Ws [COLOR="Navy"]In[/COLOR] ActiveWorkbook.Worksheets
[COLOR="Navy"]If[/COLOR] Not Ws.Name = "users" [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
   .CompareMode = vbTextCompare
[COLOR="Navy"]Set[/COLOR] Rng = Ws.Range(Ws.Range("A1"), Ws.Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
   
   [COLOR="Navy"]Set[/COLOR] Ac = Ws.Range(Dn.Offset(, 1), Ws.Cells(Dn.Row, Columns.Count).End(xlToLeft))
    
    [COLOR="Navy"]If[/COLOR] Ac.Count = 1 [COLOR="Navy"]Then[/COLOR]
        Txt = Ac.Value
    [COLOR="Navy"]Else[/COLOR]
        Txt = "(" & Join(Application.Transpose(Application.Transpose(Ac.Value)), "_") & ")"
    [COLOR="Navy"]End[/COLOR] If
          Ac = vbNullString
          
            [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
                .Add Dn.Value, Array(Dn, Txt)
                 Dn.Offset(, 1) = Txt
            [COLOR="Navy"]Else[/COLOR]
                Q = .Item(Dn.Value)
                   Q(1) = Q(1) & "," & Txt
                     Q(0).Offset(, 1).Value = Q(1)
                        [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
                            [COLOR="Navy"]Set[/COLOR] nRng = Dn
                        [COLOR="Navy"]Else[/COLOR]
                            [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dn)
                        [COLOR="Navy"]End[/COLOR] If
                .Item(Dn.Value) = Q
            [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
.RemoveAll
[COLOR="Navy"]End[/COLOR] With
 
 [COLOR="Navy"]If[/COLOR] Not nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] nRng.EntireRow.Delete
   [COLOR="Navy"]Set[/COLOR] nRng = Nothing
    Lst = 0
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Ws
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
"users" now remains untouched (which is correct), but get the same error (runtime error 13, type mismatch), and the debug highlights

Txt = "(" & Join(Application.Transpose(Application.Transpose(Ac.Value)), "_") & ")"

Again, as before, the remaining tabs are unsorted

The data on the tab the macro gets stuck on looks like this (sorry if it is unhelpful):

UserJob titleJob descriptionDate fromDate toCompany nameAbout companyCompany urlLocationReference nameReference numberReference email
1Title2012-06-242012-08-23Rest
1FugfgTest
1ProfesorOpasan posoa2008-07-242011-07-24Proshttp://skola.comsko111123tg@cc.com
1TyttTest
1TestTest
12Web DesignerWeb and graphic designer.2011-08-20Eton Digitalhttp://etondigital.comNovi Sad, Autonomna Pokrajina Vojvodina, Serbiainfo@etondigital.com
74DirectorStudents can use WCS from day 1, year 1 of their university career to create a professional profile and expose themselves to employers.
Students can choose to be mentored by industry experts offering advice pertaining to the real world and be invited to events by employers. WCS provides students with the ability to network their way to success.
2010-08-07We Connect StudentsA revolutionary recruitment portal that enables students and employers to connect.http://weconnectstudents.comLondon, England, United KingdomClive Banks7.92E+09clive@weconnectstudents.com
74FounderWe Connect Students is the new online social community which places students in the centre of a well-connected, professional environment of employers and industry mentors looking to identify and attract graduate talent.

WCS brings together students, employers and mentors in a focused, professional way while allowing criteria like social mobility to be taken into account.
2008-03-042010-08-07We Connect StudentsA powerful search enables students to be found by the right employer for the right job.http://weconnectstudents.comBeccles, England, United Kingdom
75InternPlacement as part of my degree. Worked in all hotel areas, from Maintenance to Evening Host.2011-06-062012-06-06Manor on Golden PondThe Manor has 24 guest bedrooms, a spa, tennis court and a heated outdoor swimming pool. The hotel is AAA 4 diamond and is also a member of Small Luxury Hotels and Wine Spectator.http://www.manorongoldenpond.comHolderness, New Hampshire, United StatesBrian Shieldsbrianps@roadrunnner.com
95Public Relations OfficerDesigning and publishing material for the public.
Participating in the public outreach programme "The Sun Dome", inc. Giving lectures at schools
Guiding tours for the public around the site
2011-01-032011-01-06United Kingdom Atomic Energy AuthorityLeading research company in UK and Europe for Nuclear Fusion based power.http://www.ccfe.ac.ukCulham, England, United KingdomChris Warrickchris.warrick@ccfe.ac.uk
95Sales and Marketing Team LeaderPlanning, managing and carrying out a large scale marketing campaign.
Leading a team in strategically launching a cost-effective campaign.
2012-01-26We Connect StudentsOnline social community, putting students into the centre of a well connected system of companies who are looking for prospective graduates to employ.http://www.weconnectstudents.comOliver ****insonoliver@weconnectstudents.com
95InternShadowing and administrative work with additional research into investments and presentations to the heads of department summarising my findings.2006-06-012006-07-01Prudential AssurancePrudential plc is an international financial services group with significant operations in Asia, the US and the UKhttp://www.prudential.co.ukLondon, England, United Kingdom

<colgroup><col span="15"></colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,985
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