+ Reply to Thread
Results 1 to 8 of 8

Transpose Data Set Macro / VBA help!

Hybrid View

Snaybot Transpose Data Set Macro /... 07-12-2017, 06:47 PM
LeoTaxi Re: Transpose Data Set Macro... 07-12-2017, 07:26 PM
xladept Re: Transpose Data Set Macro... 07-12-2017, 08:57 PM
YasserKhalil Re: Transpose Data Set Macro... 07-13-2017, 01:53 AM
jindon Re: Transpose Data Set Macro... 07-13-2017, 02:14 AM
thatandyward Re: Transpose Data Set Macro... 07-13-2017, 02:15 AM
Snaybot Re: Transpose Data Set Macro... 07-18-2017, 01:40 AM
xladept Re: Transpose Data Set Macro... 07-18-2017, 10:23 AM
  1. #1
    Registered User
    Join Date
    05-06-2014
    Location
    Cali or philly
    MS-Off Ver
    Excel 2003
    Posts
    60

    Transpose Data Set Macro / VBA help!

    I would like to transpose the sample data set below

    So that every time the value changes in column A it transposes the all like data to column B

    For example


    Current Data:

    x 12
    x 41
    x 41
    x 48
    x 43
    y 54
    y 14
    y qw
    a 43
    a 34
    a 15
    a qwe
    a 123
    r 12
    r 13


    Target Data:

    x 12 41 41 48 43
    y 54 14 qw
    a 43 34 15 qwe 123
    r 12 13

    Thanks!

    I have to do this for thousands of cells, having this macro would really help!

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Transpose Data Set Macro / VBA help!

    and the real data also starting with x or y or a ???


    cheers
    Leo

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Transpose Data Set Macro / VBA help!

    This is a bit clumsy but seems to work - Put this event in the sheet module:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 1 And Target.Value <> "" Then
    Application.EnableEvents = False
    Dim r As Long, c As Long, i As Long, n As Long: n = 2
    Dim Index As String, Item As String, S As String
    Index = Left(Cells(2, 1), InStr(1, Cells(2, 1), " ") - 1)
    i = InStr(1, Cells(2, 1), " ")
    Item = Right(Cells(2, 1), Len(Cells(2, 1)) - i)
    S = Index
    For r = 2 To Range("A" & Rows.Count).End(xlUp).Row
    i = InStr(1, Cells(r, 1), " ")
    Item = Right(Cells(r, 1), Len(Cells(r, 1)) - i)
    
    If Left(Cells(r, 1), i - 1) = Index Then
    S = S & "," & Item: c = c + 1: GoTo GetNext
    End If
    
    c = c + 1: Cells(n, 2).Resize(1, c) = Split(S, ","): n = n + 1
    Index = Left(Cells(r, 1), InStr(1, Cells(r, 1), " ") - 1)
    c = 0: r = r - 1: S = Index
    GetNext: Next r
    c = c + 1: Cells(n, 2).Resize(1, c) = Split(S, ",")
    End If
    Application.EnableEvents = True
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Transpose Data Set Macro / VBA help!

    Try this code (originally written by Mr. Karedog .. Credits go to him)
    Sub Test()
        Dim coll        As New Collection
        Dim arr         As Variant
        Dim v1          As Variant
        Dim v2          As Variant
        Dim str1        As String
        Dim maxItem     As Long
        Dim i           As Long
        Dim j           As Long
    
        arr = Sheets("Sheet1").Range("A1").CurrentRegion.Value
    
        For i = 1 To UBound(arr, 1)
            str1 = CStr(arr(i, 1))
    
            On Error Resume Next
                coll.Add Key:=str1, Item:=New Collection
            On Error GoTo 0
    
            If coll(str1).Count = 0 Then coll(str1).Add str1
            For j = 2 To UBound(arr, 2)
                If Len(arr(i, j)) Then coll(str1).Add CStr(arr(i, j))
            Next j
        Next i
    
        For Each v1 In coll
            If v1.Count > maxItem Then maxItem = v1.Count
        Next v1
    
        ReDim arr(1 To coll.Count, 1 To maxItem)
        i = 0
        For Each v1 In coll
            i = i + 1
            j = 0
            For Each v2 In v1
                j = j + 1
                arr(i, j) = v2
            Next v2
        Next v1
    
        Sheets("Sheet1").Range("D1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

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

    Re: Transpose Data Set Macro / VBA help!

    Quote Originally Posted by Snaybot View Post
    Target Data:

    x 12 41 41 48 43
    y 54 14 qw
    a 43 34 15 qwe 123
    r 12 13
    Sub test()
        Dim e, x, y, LR As Long, temp, n As Long
        LR = Range("a" & Rows.Count).End(xlUp).Row
        x = "a1:a" & LR
        For Each e In Filter(Evaluate("transpose(if(countif(offset(" & x & _
                ",,,row(1:" & LR & "))," & x & ")=1," & x & "))"), False, 0)
            temp = e: If Not IsNumeric(temp) Then temp = Chr(34) & temp & Chr(34)
            y = Filter(Evaluate("transpose(if(a1:a1000=" & temp & ",b1:b1000))"), False, 0)
            n = n + 1
            Cells(n, "d").Value = e
            Cells(n, "e").Resize(, UBound(y) + 1).Value = y
        Next
    End Sub

  6. #6
    Registered User
    Join Date
    08-08-2014
    Location
    Lancaster, PA
    MS-Off Ver
    2016 (windows & mac)
    Posts
    94

    Re: Transpose Data Set Macro / VBA help!

    see if this works for you. paste it into the code module of the required sheet.

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim DataRng As Range
        Dim DataArr() As Variant, i As Long
        Dim Key As String, DataValue As String
        Dim KeyStore As String, DataCol As New Collection
        Dim KeyPosition As Long, TempDataValue As String
        
        'define DataRng, Col A (+1 to account for deletion of data in last row)
        Set DataRng = Cells(1, 1).Resize(Cells(Rows.Count, 1).End(xlUp).Row + 1)
        
        'only process if change in DataRng
        If Intersect(Target, DataRng) Is Nothing Then Exit Sub
        
        'store data in 1D array
        DataArr = Application.Transpose(DataRng)
        
        'process data
        For i = LBound(DataArr) To UBound(DataArr)
        
            'only process if data present & has space delimiter
            If Not IsEmpty(DataArr(i)) And InStr(DataArr(i), " ") > 0 Then
        
                'assign current Key & DataValue pair to temp variables
                Key = Split(DataArr(i), " ")(0)
                DataValue = Split(DataArr(i), " ")(1)
            
                'test if Key already stored
                If InStr(KeyStore, Key) = 0 Then 'not stored
                
                    'add Key to KeyStore
                    KeyStore = KeyStore & "_" & Key
                
                    'add DataValue to collection incl. Key before as first entry
                    Call DataCol.Add(Key & " " & DataValue, Key)
                
                Else 'already stored
                
                    'determine postion of key in collection
                    KeyPosition = InStr(KeyStore, Key) / 2
                
                    'store matching collection item to TempDataValue
                    TempDataValue = DataCol.Item(Key)
                    
                    'delete matching collection item
                    Call DataCol.Remove(Key)
                    
                    're-create collection item with new data appended
                    If KeyPosition > DataCol.Count Then
                        Call DataCol.Add(TempDataValue & " " & DataValue, Key) 'last position
                    Else
                        Call DataCol.Add(TempDataValue & " " & DataValue, Key, KeyPosition)
                    End If
                
                End If
                
            End If
    
        Next i
        
        'redim array for output
        ReDim DataArr(1 To DataCol.Count)
        
        'copy collection to array
        For i = LBound(DataArr) To UBound(DataArr)
    
            DataArr(i) = DataCol.Item(i)
    
        Next i
        
        'clear col B
        Cells(1, 2).Resize(Cells(Rows.Count, 2).End(xlUp).Row).ClearContents
        
        'output array to col B
        Cells(1, 2).Resize(UBound(DataArr)) = Application.Transpose(DataArr)
    
    End Sub
    this splits each key/data pair on the " " delimiter, it will bypass any data which doesn't contain the " " delimiter or if the cell is empty.

    I'm using a Collection and String as a make shift Dictionary for the key/data pair of each row. it's a little clunky but as Scripting Dictionary is only available on windows machines I prefer to avoid them unless I'm sure the code will only ever be run on windows machines.
    Last edited by thatandyward; 07-13-2017 at 09:28 AM.

  7. #7
    Registered User
    Join Date
    05-06-2014
    Location
    Cali or philly
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Transpose Data Set Macro / VBA help!

    thanks guys!

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Transpose Data Set Macro / VBA help!

    You're welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Transpose data macro
    By makinmomb in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-11-2015, 05:16 PM
  2. [SOLVED] Transpose Data Using Macro
    By Jbrowning1980 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2015, 12:57 PM
  3. [SOLVED] macro to transpose data
    By rhoda20 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-08-2014, 08:39 AM
  4. [SOLVED] Macro To Transpose Data
    By AlexRoberts in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2014, 09:33 AM
  5. Help with macro to transpose data
    By ejecheche in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-03-2012, 06:26 AM
  6. Transpose Data Macro
    By Jakila2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2011, 03:56 PM
  7. Macro to Transpose the data
    By pandyav in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-10-2010, 05:51 AM

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