+ Reply to Thread
Results 1 to 2 of 2

Sorting data in coloums based on another coloum - Code Continuation

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-25-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2003
    Posts
    179

    Sorting data in coloums based on another coloum - Code Continuation

    Hello All,
    I wanted to know how to sort a list of data in a row based on a number in the colum next to it and then by Alphabetical order.

    So for example Coloum I contains the numbers 2, 1.2 and 1.1
    I need it so that everything if I says 1.1 then the coressponding J cell is placed in a list bellow A3. This should then be repeated for all values of 1.1. Then 1.2 should go in B3 and bellow and 2 should go in C3.

    So far i've written this bit that collects and sorts data in J based on I, but im not sure how to continue it to split it up under the A,B,C coloums in the same alphabetical order.

    Code so far
    Range("A19:J250").Select
        ActiveWindow.SmallScroll Down:=-156
        Selection.Sort Key1:=Range("I20"), Order1:=xlDescending, Key2:=Range( _
            "J20"), Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
            DataOption2:=xlSortNormal
    Test file here to show the sort Test book1.xls
    Last edited by joshnathan; 07-16-2012 at 04:30 AM.

  2. #2
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Sorting data in coloums based on another coloum - Code Continuation

    A bit of code for you. Go to the VBA IDE (alt-F11), insert a new module, and paste in this code:

    Option Explicit
    
    Public Sub btnSortSplit_Click()
    
        Call SortSplit(Worksheets("Sheet1").Range("I19:J19"), Worksheets("Sheet1").Range("A2"))
    
    End Sub
    
    Public Sub SortSplit(ByVal rngTarget As Excel.Range, ByVal rngDest As Excel.Range)
    
        Dim arrData         As Variant
    
        Dim lngLastRow      As Long
        Dim lngLastCol      As Long
        Dim lngCurrRow      As Long
        Dim lngStartRow     As Long
        Dim lngRow2         As Long
        Dim intCol1         As Integer
        Dim intCol2         As Integer
        Dim varOutput       As Variant
    
        intCol1 = rngDest.Column
        lngLastCol = rngDest.Parent.Rows(rngDest.Row).Find(What:="*", _
            After:=Cells(rngDest.Row, 1), _
            SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, _
            LookAt:=xlPart, LookIn:=xlValues).Column
        arrData = rngDest.Cells(1, 1).Resize(1, lngLastCol - intCol1 + 1)
        For intCol2 = LBound(arrData, 2) To UBound(arrData, 2)
            If arrData(1, intCol2) = "" Then
                Exit For
            End If
        Next intCol2
        If intCol2 > UBound(arrData, 2) Then
            intCol2 = UBound(arrData, 2)
        End If
        lngLastRow = rngDest.Parent.Columns(intCol1).Resize(, intCol2).Find _
            (What:="*", After:=rngDest.Cells(1, 1), _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
            LookAt:=xlPart, LookIn:=xlValues).Row
        rngDest.Resize(lngLastRow - rngDest.Row + 1, intCol2).ClearContents
    
        intCol2 = rngTarget.Column
        lngLastRow = rngTarget.Parent.Columns(intCol2).Find(What:="*", _
            After:=Cells(1, intCol2), _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
            LookAt:=xlPart, LookIn:=xlValues).Row
        rngTarget.Resize(lngLastRow - rngTarget.Row + 1, 2).Sort _
            Key1:=rngTarget.Cells(1, 1), Order1:=xlAscending, _
            Key2:=rngTarget.Cells(1, 2), Order2:=xlAscending, _
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
            DataOption2:=xlSortNormal
    
        arrData = rngTarget.Offset(1, 0).Resize(lngLastRow - rngTarget.Row, 2)
        varOutput = arrData(1, 1)
        lngStartRow = LBound(arrData)
        For lngCurrRow = LBound(arrData) + 1 To UBound(arrData)
            If arrData(lngCurrRow, 1) <> varOutput Then
                rngDest.Offset(0, intCol1 - 1) = arrData(lngStartRow, 1) & "'s"
                rngDest.Offset(1, intCol1 - 1).Resize(lngCurrRow - lngStartRow, 1).Value _
                    = rngTarget.Offset(lngStartRow, 1).Resize(lngCurrRow - lngStartRow, 1).Value
                intCol1 = intCol1 + 1
                lngStartRow = lngCurrRow
                varOutput = arrData(lngCurrRow, 1)
            End If
        Next lngCurrRow
        rngDest.Offset(0, intCol1 - 1) = arrData(lngStartRow, 1) & "'s"
        rngDest.Offset(1, intCol1 - 1).Resize(lngCurrRow - lngStartRow, 1).Value _
            = rngTarget.Offset(lngStartRow, 1).Resize(lngCurrRow - lngStartRow, 1).Value
    
        Set rngTarget = Nothing
        Set rngDest = Nothing
    
    End Sub
    The btnSortSplit_Click procedure calls SortSplit, passing it the header range of the source data and the first cell of the destination. The SortSplit procedure first identifies and clears out any previous results in the destination, sorts the source data, the loops through the sorted source data looking for changes. As changes are found, they are copied to the destination.

    Assign the btnSortSplit_Click to a button on your sheet, and you are good to go.

+ 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