Consulting

Results 1 to 2 of 2

Thread: Using VBA to sort data

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    28
    Location

    Using VBA to sort data

    I have inherited a set of codes used in another workbook for sorting within a protected workbook. I am trying to amend this codes to suit the workbook I am using. The differences I can see are the sort order is in Column I and descending order whilst the code I have is sort based on Column A and ascending order. I tried the codes by changing to :

    Sort Key1=.Range( "I8") and Order1:=xlDescending.

    Somehow I could not get it to work. Please help to see what I need to get this right.

    Sub Sort()
    
    Dim lngLastRow As Long
        
        lngLastRow = Cells.Find(What:="*", After:=[A1], _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious).Row
                                
        With Sheets("Summary")
            
            .Range("A8:P" & lngLastRow).Sort Key1:=.Range("A8"), _
                                              Order1:=xlAscending, _
                                              Header:=xlNo, _
                                              OrderCustom:=1, _
                                              MatchCase:=False, _
                                              Orientation:=xlTopToBottom
            
    
        End With
    End Sub

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    make sure the summary sheet is active and see if this gets you close:

    [VBA]Cells.Select
    Selection.Sort Key1:=Range("I1"), Order1:=xlDescending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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