+ Reply to Thread
Results 1 to 5 of 5

Sort Range Problem

  1. #1
    Registered User
    Join Date
    01-19-2013
    Location
    Waterloo, ON, Canada
    MS-Off Ver
    Excel 2007
    Posts
    3

    Sort Range Problem

    I refresh the data in my sheet frequently, and use a macro to sort it after importing.

    When I Record the macro, it works the first time, but it has hardcoded Ranges.

    Please Login or Register  to view this content.
    Note that the Add Key and SetRange coordinates are fixed. The bottom Row will be incorrect the next time because there may be more, or fewer, imported rows.
    What code do I need in the blank row to sense the bottom row, and how do I use it for the Add Key and SetRange specifications?

    Any help would be very appreciated.

    Moderators Note:
    • Please follow Forum Rule #3 and use code tags.
    • Added this time, but please use them in the future…Thanks.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Sort Range Problem

    Hi,

    Please see the almost identical point made in post #4 of this http://www.excelforum.com/excel-prog...y-of-rows.html thread which I posted just now.

    You should create a dynamic range name and use the name in your macro.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    01-19-2013
    Location
    Waterloo, ON, Canada
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Sort Range Problem

    Yes, I see it now. Thank you, Richard.

    I was just coming back to report that I'd found a solution - which is similar in concept to your ideas.

    Please Login or Register  to view this content.
    - and now I see your followup about the value of dynamic range names. I'll study that too.

    Thanks again for your help.

    - Fred
    Last edited by xlfred; 01-21-2013 at 03:45 PM.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Sort Range Problem

    Hi,

    As a general principle it's always more efficient to use standard Excel functionality rather than VBA code. Which is why a dynamic Excel range name is better.

    What you have will of course work but you are open to problems with someone adding a row and your code not finding the last row correctly, not to mention having to use string slicing code in the macro.

    Dynamic range names are simple and once created you can forget about them.

  5. #5
    Registered User
    Join Date
    01-19-2013
    Location
    Waterloo, ON, Canada
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Sort Range Problem

    Yes! Very nice, Richard.

    Please Login or Register  to view this content.
    - Fred

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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