+ Reply to Thread
Results 1 to 10 of 10

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

Hybrid View

arcadian13 Match cells in column, then... 07-21-2012, 07:57 PM
jindon Re: Match cells in column,... 07-21-2012, 10:23 PM
arcadian13 Re: Match cells in column,... 07-22-2012, 09:13 AM
jindon Re: Match cells in column,... 07-22-2012, 09:27 AM
arcadian13 Re: Match cells in column,... 07-22-2012, 09:50 AM
jindon Re: Match cells in column,... 07-22-2012, 10:09 AM
arcadian13 Re: Match cells in column,... 07-22-2012, 10:12 AM
jindon Re: Match cells in column,... 07-22-2012, 10:31 AM
arcadian13 Re: Match cells in column,... 07-22-2012, 12:03 PM
arcadian13 Re: Match cells in column,... 07-22-2012, 11:37 AM
  1. #1
    Registered User
    Join Date
    07-21-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    6

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

    The link

    http://www.excelforum.com/excel-prog...b-c-d-etc.html

    has good code which works for me (thanks tigeravatar). I have a workbook with a number of tabs, and I want to apply the code to each tab in one shot. I have no idea about programming, so any help appreciated!

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

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

    try
    Option Explicit
    
    Sub test()
        Dim ws As Worksheet, a, i As Long, maxCol As Long, w
        For Each ws In Worksheets
            With ws.Range("a1").CurrentRegion
                a = .Resize(, 2).Value
                ReDim Preserve a(1 To UBound(a, 1), 1 To 100)
                With CreateObject("Scripting.Dictionary")
                    .CompareMode = 1
                    For i = 3 To UBound(a, 1)
                        If Not .exists(a(i, 1)) Then
                            .Item(a(i, 1)) = VBA.Array(i, 2)
                        Else
                            w = .Item(a(i, 1))
                            w(1) = w(1) + 1
                            If w(1) > UBound(a, 2) Then
                                ReDim Preserve _
                                a(1 To UBound(a, 1), 1 To UBound(a, 2) + 100)
                            End If
                            a(w(0), w(1)) = a(i, 2)
                            a(i, 2) = Empty
                            .Item(a(i, 1)) = w
                            maxCol = Application.Max(maxCol, w(1))
                        End If
                    Next
                End With
                .Resize(, maxCol).Value = a
            End With
        Next
    End Sub

  3. #3
    Registered User
    Join Date
    07-21-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    6

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

    Many thanks for your solution, which seems very elegant.

    (1) I tested it with two tabs, each tab has two columns of data, works great. Is it possible to have the results eliminate the redundant rows?

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

    I currently get

    1 a b
    1
    2 a c d
    2
    2

    (3) I have some tabs with more than two columns of data (there is no set rule as to how many columns there might be). Will it work with this?

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

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

    Need to see the data and the result that you want.

  5. #5
    Registered User
    Join Date
    07-21-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    6

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

    Sure, I am attaching an example of a multi tab, multi-column-in-tab sheet that I am hoping to sort in the way described
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

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

    What is your desired result?

  7. #7
    Registered User
    Join Date
    07-21-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    6

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

    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
    1 c d
    2 e f
    2 g h

    Would like to get

    1 a b c d
    2 e f g h

    Really appreciate this!

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

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

    See if this is what you wanted
    
    Sub test()
        Dim ws As Worksheet, a, i As Long, ii As Long, maxCol As Long, w, x
        For Each ws In Worksheets
            With ws.Range("a1").CurrentRegion
                a = .Value
                If IsArray(a) Then
                x = UBound(a, 2)
                    ReDim Preserve a(1 To UBound(a, 1), 1 To 100)
                    With CreateObject("Scripting.Dictionary")
                        .CompareMode = 1
                        For i = 2 To UBound(a, 1)
                            If Not .exists(a(i, 1)) Then
                                .Item(a(i, 1)) = VBA.Array(i, x)
                            Else
                                w = .Item(a(i, 1))
                                w(1) = w(1) + 1
                                If w(1) + x > UBound(a, 2) Then
                                    ReDim Preserve _
                                    a(1 To UBound(a, 1), 1 To UBound(a, 2) + 100)
                                End If
                                For ii = 2 To x
                                    a(w(0), w(1) + ii - 2) = a(i, ii)
                                    a(i, ii) = Empty
                                Next
                                .Item(a(i, 1)) = w
                                maxCol = Application.Max(maxCol, w(1) + x)
                            End If
                        Next
                    End With
                    If maxCol > 0 Then
                        .Resize(, maxCol).Value = a
                    End If
                End If
            End With
        Next
    End Sub

  9. #9
    Registered User
    Join Date
    07-21-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    6

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

    Actually, the code still seems to be cutting off some of the data. See the attached before and after examples
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    07-21-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    6

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

    That is great. Many thanks.

    Is it possible to remove the redundant rows as in my example? At the moment:

    1 a
    1 b
    1 c
    2 a
    2 c
    2 d

    Turns into

    1 a b c
    1
    2 a c d
    2
    2

    I would like to get to

    1 a b
    2 a c d

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1