+ Reply to Thread
Results 1 to 4 of 4

Getting the correct Syntax for Range in Sorting

Hybrid View

highlystrung Getting the correct Syntax... 08-30-2011, 04:16 PM
TMS Re: Getting the correct... 08-30-2011, 04:22 PM
highlystrung Re: Getting the correct... 08-30-2011, 04:30 PM
TMS Re: Getting the correct... 08-30-2011, 04:50 PM
  1. #1
    Forum Contributor
    Join Date
    04-13-2011
    Location
    Havant, Hants, England
    MS-Off Ver
    Excel 2010
    Posts
    116

    Getting the correct Syntax for Range in Sorting

    Hi all,

    I'm trying to get the correct syntax for identifying the range in a variable sized table on sorting. The line using a fixed range reference - Range("E2:E296") - works but doesn't cater for the changing table size. I've tried syntax in the line above to correct this but it's giving me an error. Can anyone advise what the correct syntax is please,

    thanks, Neil

    Sub a1()
       
        
        With ActiveWorkbook.Worksheets("Analysis Sheet").Sort      
            .SortFields.Clear
            .SortFields.Add Key:=Range("d2", ActiveCell.End(xlDown)).Address,
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal            
            .SortFields.Add Key:=Range("E2:E296"), SortOn:=xlSortOnValues,                         Order:=xlAscending, DataOption:=xlSortNormal         
            .SetRange DataRange            
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply                                      
        End With
        
      
    End Sub
    Last edited by highlystrung; 08-30-2011 at 04:30 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,515

    Re: Getting the correct Syntax for Range in Sorting

    Try:

    Range("E2:E" & Range("E" & Rows.Count).End(xlDown).Row)


    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    04-13-2011
    Location
    Havant, Hants, England
    MS-Off Ver
    Excel 2010
    Posts
    116

    Re: Getting the correct Syntax for Range in Sorting

    That seems to do the trick, thanks alot. How do you learn / work out these complex syntaxes....!?! regards, Neil

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,515

    Re: Getting the correct Syntax for Range in Sorting

    Well, once upon a time, I was asking exactly the same questions. It just takes time and practice. Monitoring the questions and answers in the forum is a great way to learn ... and asking questions.

    Regards

+ 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