Consulting

Results 1 to 7 of 7

Thread: Solved: Macro to sort data ascending

  1. #1
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location

    Solved: Macro to sort data ascending

    Each example contains 21 rows starting in row 66. Each example has one ticker that is either Long or Short and 20 tickers that are either a Long Basket or Short Basket.

    For each example I would like to sort ascending the 20 tickers that are either a Long Basket or Short Basket.


    Any help would be great. Thanks!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    So you want to sort B66:S86 (equivalent) by column B ascending in each group?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location
    No. For each group I would like to sort F67:F86 ascending and that is all

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Part working, no time to figure out the bug!
    [VBA]
    Sub Dosort()
    Dim c As Range, Rng As Range
    Set c = Range("A66")
    Set Rng = c.Offset(1, 5).Resize(20)
    ActiveSheet.Sort.SortFields.Clear
    Do
    Set Rng = c.Offset(1, 5).Resize(20)
    Rng.Select
    With ActiveSheet.Sort
    .SortFields.Add Key:=Rng(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange Rng
    .Header = xlNo
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    Set c = c.End(xlDown)
    Loop Until c.Row = Rows.Count
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location
    Thanks but how am I supposed to figure out how to fix the bug? I have no idea!?

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    Option Explicit
    Sub Dosort()
    Dim c As Range, Rng As Range
    Set c = Range("A66")
    Set Rng = c.Offset(1, 5).Resize(20)
    Do
    Set Rng = c.Offset(1, 5).Resize(20)
    Rng.Sort Key1:=Rng(1), Order1:=xlAscending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Set c = c.End(xlDown)
    Loop Until c.Row = Rows.Count
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location
    Thank you

Posting Permissions

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