+ Reply to Thread
Results 1 to 4 of 4

need help!! sort multiple rows into single column

Hybrid View

  1. #1
    Registered User
    Join Date
    12-27-2015
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    6

    need help!! sort multiple rows into single column

    Hi,

    I am using excel 2010 and windows.
    can anyone help translate using VBA code.
    thank you.

    here is some example data
    description header1 header2 header3 header4 header5
    test 2 8 9 69 47
    trial 1 5 99 102 1010
    fry 3 5 4 7 10

    result that I want is
    Description value
    trial 1
    test 2
    fry 3
    fry 4
    fry 5
    trial 5
    fry 7
    test 8
    test 9
    fry 10
    test 47
    test 69
    trial 99
    trial 102
    trial 1010

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: need help!! sort multiple rows into single column

    You could use this code that read data in the first sheet and copy and sort them in the second sheet:
    Sub macro1()
       Dim sh1 As Worksheet
       Dim sh2 As Worksheet
       Dim lastRow As Long, r As Long
       Dim lastCol As Integer, destRow As Long
       
       With ThisWorkbook
          Set sh1 = .Sheets(1)
          Set sh2 = .Sheets(2)
       End With
       
       lastRow = sh1.Cells(Rows.Count, 1).End(xlUp).Row
       destRow = 2
       For r = 2 To lastRow
          lastCol = sh1.Cells(r, Columns.Count).End(xlToLeft).Column
          
          sh1.Cells(r, 2).Resize(, lastCol - 1).Copy
          sh2.Cells(destRow, 2).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
          Application.CutCopyMode = False
          sh2.Cells(destRow, 1).Resize(lastCol - 1) = sh1.Cells(r, 1)
          
          destRow = destRow + lastCol - 1
       Next r
       
       'sort
       sh2.Sort.SortFields.Clear
       sh2.Sort.SortFields.Add Key:=Range("B:B"), _
          SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
       With sh2.Sort
          .SetRange Range("A:B")
          .Header = xlYes
          .MatchCase = False
          .Orientation = xlTopToBottom
          .SortMethod = xlPinYin
          .Apply
       End With
    End Sub
    Regards,
    Antonio

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

    Re: need help!! sort multiple rows into single column

    Assuming header starts from A1 and no blank row(s)/column(s) in data range.
    Sub test()
        Dim a, b, i As Long, ii As Long, n As Long
        With Cells(1).CurrentRegion
            a = .Value
            ReDim b(1 To Application.CountA(.Offset(1, 1)), 1 To 2)
            For i = 2 To UBound(a, 1)
                For ii = 2 To UBound(a, 2)
                    If a(i, ii) <> "" Then
                        n = n + 1
                        b(n, 1) = a(i, 1): b(n, 2) = a(i, ii)
                    End If
                Next
            Next
            With .Offset(, .Columns.Count + 2).Resize(n, 2)
                .Value = b
                .Sort .Cells(1, 2), 1, , .Cells(1, 1), 1
            End With
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    12-27-2015
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    6

    Re: need help!! sort multiple rows into single column

    thanks both of you for your helps.

    both code are working and really appreciate it.

+ 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. Replies: 9
    Last Post: 11-11-2015, 08:19 PM
  2. Transpose Multiple Rows into a Single Column
    By vnascimento in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-04-2013, 08:47 AM
  3. Sort first column of multiple workbooks from a single directory
    By lmolokin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2012, 05:15 PM
  4. Replies: 0
    Last Post: 09-26-2012, 10:33 AM
  5. [SOLVED] Importing CSV file into single column/multiple rows vs. multiple rows/single column
    By bjorgenson@charter.net in forum Excel General
    Replies: 4
    Last Post: 07-03-2012, 08:01 PM
  6. Turn multiple rows into a single column
    By mckaa302 in forum Excel General
    Replies: 5
    Last Post: 05-26-2012, 08:08 PM
  7. Replies: 5
    Last Post: 02-07-2012, 04:55 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