+ Reply to Thread
Results 1 to 4 of 4

VBA Script to Sort a row in ascending order, then go to next row and repeat

Hybrid View

  1. #1
    Registered User
    Join Date
    09-09-2011
    Location
    Arizona, United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    VBA Script to Sort a row in ascending order, then go to next row and repeat

    I have thousands of rows of 5 numbers in columns B-F that I need to sort horizontally in ascending order. I am trying to use VBA for this because I want each individual line to be in order.

    For an example.

    22 5 38 99 01 will need to be 01 05 22 38 99

    and so on for the next row under neath it. I tried recording a code but it keeps the rows in my recording. I'd like it to go to the next row "X" and sort for columns B-F and then repeat about 2000 times.

    Any suggestions on how to rewrite this?

    Thanks in advance.


    
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
        ActiveWorkbook.Worksheets("history").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("history").Sort.SortFields.Add2 Key:=Range _
            ("B114:F114"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("history").Sort
            .SetRange Range("B114:F114")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("B115:F115").Select
        ActiveWorkbook.Worksheets("history").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("history").Sort.SortFields.Add2 Key:=Range _
            ("B115:F115"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("history").Sort
            .SetRange Range("B115:F115")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("B116:F116").Select
        ActiveWorkbook.Worksheets("history").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("history").Sort.SortFields.Add2 Key:=Range _
            ("B116:F116"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("history").Sort
            .SetRange Range("B116:F116")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: VBA Script to Sort a row in ascending order, then go to next row and repeat

    Code for macro for sorting rows from 114 till end one by one.
    Sub SortHorizontal()
    Dim LR As Long, T As Long
    
    LR = Range("B" & Rows.Count).End(xlUp).Row
    
    For T = 114 To LR
    ActiveSheet.Sort.SortFields.Clear
        
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B" & T & ":F" & T), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("B" & T & ":F" & T)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    Next T
        
    End Sub
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

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

    Re: VBA Script to Sort a row in ascending order, then go to next row and repeat

    Try
    Sub test()
        Dim i As Long
        Application.ScreenUpdating = False
        For i = 114 To Sheets("history").Range("a" & Rows.Count).End(xlUp).Row
            With Sheets("history").Rows(i).Resize(, 6)
                .Sort .Rows(1), 1, Orientation:=2
            End With
        Next
        Application.ScreenUpdating = True
    End Sub

  4. #4
    Registered User
    Join Date
    09-09-2011
    Location
    Arizona, United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: VBA Script to Sort a row in ascending order, then go to next row and repeat

    These work, thank you guys!

+ 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. [SOLVED] Sort in ascending order with same mark
    By kent97 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-05-2016, 08:55 AM
  2. Sort the final digits and sort in ascending order
    By jorel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-29-2014, 11:58 AM
  3. [SOLVED] Sort in ascending order
    By Glenn Kennedy in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-07-2013, 08:11 AM
  4. [SOLVED] Sort Ascending order
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2013, 02:27 PM
  5. [SOLVED] Sort in ascending order
    By Dibbley247 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-17-2013, 12:30 AM
  6. sort ascending order
    By iscar_marius in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 02-11-2009, 11:00 AM
  7. eliminate repeat values from a list of ascending order
    By norumbegan in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-14-2006, 09:05 PM

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