+ Reply to Thread
Results 1 to 8 of 8

Transpose vertical data looping through entire column

Hybrid View

  1. #1
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Transpose vertical data looping through entire column

    I need help with this. I have data in ColA that is separated by a blank cell. The data is never the same number of rows so the only constant is the empty rows between the data. I need to transpose that data over and over again into ColB all the way till there is no more data. Here is a sample of my data:

    ColA
    data
    data
    data

    data
    data
    data
    data

    data
    data

    etc.

    I want the result to look similar to this:

    ColB ColC ColD ColE
    data data data
    data data data data
    data data
    etc
    Last edited by PY_; 09-06-2013 at 06:23 AM.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Transpose vertical data looping through entire column

    Try:

    Sub RunMe()
    Dim LR As Long, StartRow As Long, EndRow As Long
    Dim myRange As Range
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    StartRow = 1
    Application.ScreenUpdating = False
    
    Do
        If Range("A" & StartRow).Offset(1, 0).Value = "" Then
            Set myRange = Range("A" & StartRow)
            EndRow = Range("A" & StartRow).Row
        Else
            EndRow = Range("A" & StartRow).End(xlDown).Row
            Set myRange = Range("A" & StartRow, "A" & EndRow)
        End If
    
    myRange.Copy
    
    If Range("B1").Value = "" Then
        Range("B1").PasteSpecial Paste:=xlPasteAll, Transpose:=True
    Else
        Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteAll, Transpose:=True
    End If
    StartRow = EndRow + 2
    
    Loop Until EndRow = LR
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub
    Last edited by stnkynts; 09-05-2013 at 11:24 PM.

  3. #3
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Transpose vertical data looping through entire column

    Hi,

    Try this... it may not be the most elegant solution but it seems to work :
    Sub TransposeData()
       Dim i As Long, j As Long, ar, n As Long
    
       n = Range("A" & Rows.Count).End(xlUp).Row + 1
       ReDim ar(1 To n, 1 To 1)
       n = 0
       For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
          If Cells(i, 1) <> "" Then
             n = n + 1
             ar(n, 1) = Cells(i, 1)
    
             j = 2
             i = i + 1
             Do
                ReDim Preserve ar(1 To UBound(ar, 1), 1 To UBound(ar, 2) + 1)
                ar(n, j) = Cells(i, 1)
                i = i + 1
                j = j + 1
             Loop Until Cells(i, 1) = ""
          End If
    
       Next i
       Cells(1, 2).Resize(n, UBound(ar, 2)) = ar
    
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

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

    Re: Transpose vertical data looping through entire column

    Sub test()
        Dim i As Long
        For i = 1 To Columns(1).SpecialCells(2).Areas.Count
            Columns(1).SpecialCells(2).Areas(i).Copy
            Cells(i, 3).PasteSpecial Transpose:=True
        Next
    End Sub

  5. #5
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Transpose vertical data looping through entire column

    Thank you all for the help! Every solution provided worked as desired.

    I ended up using jindon's solution simply because it ran very fast. I have a ton of data and the other two macros did take quite a bit of time to run.

  6. #6
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Transpose vertical data looping through entire column

    Even though jindon's solution is the fastest and it does work great, it still takes almost 2 minutes to run on a decent computer with the data i have. Is there perhaps another way of doing it?

    Things i turn off before running the script: calculation, screenupdating, events

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

    Re: Transpose vertical data looping through entire column

    Try this one
    Sub test()
        Dim i As Long, myAreas As Areas
        Set myAreas = Columns(1).SpecialCells(2).Areas
        For i = 1 To myAreas.Count
            With myAreas(i)
                Cells(i, 3).Resize(, .Rows.Count).Value = _
                Application.Transpose(.Value)
            End With
        Next
    End Sub

  8. #8
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Transpose vertical data looping through entire column

    Wow, big thanks!

    I ran the two scripts on my home laptop (not as powerful as work). I added a timer to each for comparison.

    The old script : 240 seconds
    The new script: 0.18 seconds.

    I think ill use the new one

+ 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 Vertical Data to Horizontal
    By Randu555 in forum Excel General
    Replies: 5
    Last Post: 04-18-2013, 05:05 PM
  2. Looping through the cells in a column and transpose
    By skpandian in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-07-2013, 06:08 AM
  3. Replies: 1
    Last Post: 10-03-2012, 02:46 PM
  4. [SOLVED] Transpose Horizontal to Vertical with ID Column
    By galaxycoff in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-04-2012, 10:16 PM
  5. [SOLVED] transpose data from horizontal to vertical in a specific column
    By elaine in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2006, 12:10 PM

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