+ Reply to Thread
Results 1 to 4 of 4

Macro to sort data

Hybrid View

  1. #1
    Registered User
    Join Date
    06-11-2014
    Posts
    12

    Macro to sort data

    Hi,

    I'm preparing a template for sorting information which we get from a report downloaded from our database. The report is always of a different length and my sorting buttons don't always work (if the report is to short or too long). I've created three different macros for sorting, they are the same just sort by a different column:

    Sub Sort_by_Invoice()
    '
    ' Sort_by_Invoice
    '

    '
    Range("A2:K2").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Claim").SORT.SortFields.Clear
    ActiveWorkbook.Worksheets("Claim").SORT.SortFields.Add Key:=Range("D3:D1530") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Claim").SORT
    .SetRange Range("A2:K1530")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    End Sub

    The problem is, the selection button doesn't work the way I want. When I created the macro I highlighted the top row and then pressed Ctrl+Shift+down arrow thinking this would work for the different lengths of the report. Well, it doesn't work. The sorting is all over the place.

    Now, I was thinking maybe I could use a Do...Until command with something saying until the active cell at the bottom of the report is empty. But I am not sure how to write the code so that it works.

    Can anyone help, please?

  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,099

    Re: Macro to sort data

    Please use Code Tags around your code.

    Sub Sort_by_Invoice()
    '
    ' Sort_by_Invoice
    '
    
    '
    Dim lLR As Long
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    
    
    ActiveWorkbook.Worksheets("Claim").SORT.SortFields.Clear
    ActiveWorkbook.Worksheets("Claim").SORT.SortFields.Add Key:=Range("D3:D" & lLR) _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Claim").SORT
    .SetRange Range("A2:K" & lLR)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    End Sub

    Regards, TMS
    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
    Registered User
    Join Date
    06-11-2014
    Posts
    12

    Re: Macro to sort data

    Wow! Thank you! It worked brilliantly!

    Thanks!!!

  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,099

    Re: Macro to sort data

    You're welcome.

+ 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. Using a Macro in Excel to Sort Data in 2 columns and align the rows with like data
    By GGarrett in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-08-2013, 09:07 PM
  2. [SOLVED] Macro to sort data in spreadsheet but maintain empty rows between data.
    By vzc8 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 06-12-2012, 12:31 AM
  3. How to sort data with macro or VBA
    By Benjamin2008 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-18-2010, 06:38 PM
  4. Macro to sort through data
    By systemx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2006, 02:10 AM
  5. Using a MACRO to sort data
    By BAM718 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-16-2005, 09:08 AM

Tags for this Thread

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