+ Reply to Thread
Results 1 to 2 of 2

sorting through multiple columns vba

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2012
    Location
    virginia
    MS-Off Ver
    Excel 2007
    Posts
    2

    sorting through multiple columns vba

    Hello, I have a table with 5 columns of data and 4 rows of line. I need to sort them by their date column in ascending order. Can someone please help me with the code? I searched through several other forums but I could not resolve it. Also, please note that recording a macro won't work since my data varies and using the sort function in Excel is not an option for me. I attached a modified screenshot of my data (for security purposes). Thank you in advance.34.png

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: sorting through multiple columns vba

    Hello tinzpro,

    Welcome to the Forum!

    In future posts, please attach a workbook rather than a picture. Posting a workbook will get you an answer quicker. People are not to inclined to take the time to recreate your work. This macro should do what need. Let me the results.
    Sub SortByEarliestDate()
    
        Dim Rng As Range
        Dim RngEnd As Range
        Dim Wks As Worksheet
        
            Set Wks = ActiveSheet
            
            Set Rng = Wks.Range("A1").EntireColumn
            
                Set Rng = Rng.Resize(ColumnSize:=Wks.Cells(1, Columns.Count).End(xlToLeft).Column - Rng.Column + 1)
                Set RngEnd = Rng.Find("*", , xlFormulas, xlWhole, xlByRows, xlPrevious, False)
                Set Rng = Rng.Resize(RowSize:=RngEnd.Row - Rng.Row + 1)
                         
                With Wks.Sort
                    .SortFields.Clear
                    .SortFields.Add Key:=Rng.Offset(1, 0).Resize(Rng.Rows.Count - 1, 1), _
                                    SortOn:=xlSortOnValues, Order:=xlAscending, _
                                    DataOption:=xlSortNormal
                    .SetRange Rng
                    .Header = xlYes
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
                
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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