+ Reply to Thread
Results 1 to 3 of 3

Variable Ranges when sorting

  1. #1
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Variable Ranges when sorting

    I'm a newbee when it comes to macros. I inherited a spreadsheet with a bunch of macros and have successfully ported the macros to a new worksheet. A change that I can't figure out is how to have a variable range when doing a sort. The macro is below. The data that it works with is variable in length. Currently the sort is limited to the first 300 rows. I want to change that so it sorts through all the data, but that changes daily when the macro is run. How would I code it for a variable range (blank after the last row).

    Sub Correct_Backlog_Sort()
    '
    ' Correct_Backlog_Sort Macro
    ' Resort based on corrected backlog
    '

    '
    Cells.Select
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("C2:C300") _
    , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range( _
    "AE2:AE300"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range( _
    "AF2:AF300"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
    xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
    .SetRange Range("A1:AF300")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Variable Ranges when sorting

    Hi..

    You need to ascertain the Last used row..

    I am assuming column A will contain data that will indicate the Last used row..

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Variable Ranges when sorting

    Thanks! Works like a Charm!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sorting 2 data ranges by comparing one column in each and sorting to match
    By MDKsmiffy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-17-2013, 03:30 PM
  2. Object variable or With block variable not set Error during sorting program
    By BlahBlahBlahBlahBlah in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-17-2012, 07:57 AM
  3. Sorting Value Ranges
    By inspiresit in forum Excel General
    Replies: 5
    Last Post: 06-15-2010, 05:54 PM
  4. Sorting ranges
    By timbim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2009, 10:59 AM
  5. [SOLVED] Counting variable ranges and auto-summing variable ranges
    By Father Guido in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2006, 11:10 PM

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