Results 1 to 3 of 3

Thread: VBA - Sorting by columns

  1. #1
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14

    VBA - Sorting by columns

    I use the code below to sort my data by rows -- is there a way I can sort columns - based upon numeric values in a row --- I suppose I could transpose the data then sort - then transpose the data again



    Code:
    Public HeaderRow as long
    Sub SortAscending()
        Select Case ActiveCell.Row
            Case HeaderRow
                If IsEmpty(ActiveCell.Value) Then Exit Sub
                Static MySortType As Integer
                MySortType = xlAscending
                ActiveCell.CurrentRegion.Offset(1).Sort Key1:=ActiveCell, Order1:=MySortType, Header:=xlYes
        End Select
    End Sub
    xl2007 - Windows 7
    xl hates the 255 number

  2. #2
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    12
    The sorting orientation is one of the sort method's arguments.
    To sort by columns use:

    Code:
    Sub SortAscending()
       Sheet1.Cells(1).CurrentRegion.Sort Sheet1.Cells(1), 2, , , , , , 1, , , 2
    End Sub

  3. #3
    Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0
    Hi Rasm,

    Here is my code that i had used in one of my project where i had to sort the Columns based on the date in their first cell value that were containing date in. As you see the range starts at G1 upto it last non blank cell to its right.Then this range got sorted on the behalf of the data in it . Date as data in my case.


    Code:
    Public Sub SortDate_Columns()
        
        Dim rngDate     As Range
        Dim rngCell     As Range
        Dim intCol      As Integer
        
        With ThisWorkbook.Worksheets("TempCalls")
            Set rngDate = .Range("G1", .Range("G1").End(xlToRight))
            Set rngDate = Intersect(rngDate.CurrentRegion, rngDate.CurrentRegion.Offset(0, 6))
            With rngDate
                .Sort .Cells(1), 1, , , , , , xlYes, , , 2
            End With
        End With
        Set rngDate = Nothing
        Set rngCell = Nothing
    End Sub

Similar Threads

  1. How to Add Custom List And Use in Custom Sorting
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 06-07-2013, 10:41 PM
  2. Replies: 2
    Last Post: 02-23-2013, 09:18 PM
  3. Validating 2 Columns using excel VBA
    By freakszzy in forum Excel Help
    Replies: 2
    Last Post: 07-26-2012, 12:46 PM
  4. Data sorting & Graph making according filters
    By leopaulc in forum Excel Help
    Replies: 2
    Last Post: 04-05-2012, 08:56 AM
  5. Split Range into Multiple Columns VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 3
    Last Post: 03-07-2012, 10:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •