+ Reply to Thread
Results 1 to 9 of 9

Macro to select a data range then apply a custom sort

  1. #1
    Registered User
    Join Date
    05-17-2012
    Location
    Glendale, AZ
    MS-Off Ver
    Excel 2007
    Posts
    2

    Macro to select a data range then apply a custom sort

    First I apologize for the long post that will probably need even FURTHER information that I didn't realize I need to include. Complete newbie & I have been researching this all day & have seen numerous posts with solutions for things "similar" to what I need. The problem is I've never even looked at VBA before today & I don't understand the code enough to modify it for my needs. I think some of the problem is also that I am working in an excel file that was poorly constructed before I took over managing it. I have attached the file with sensitive info removed for reference. The file is a list of short sales active with my company, grouped by branch, all on one sheet. I am looking for a macro that will dynamically select the rows for each branch & then apply a custom sort with 4 levels in the order: 1. Column K (A-Z), 2. Column J (Newest to Oldest) 3. Column I (Newest to Oldest) & 4. Column F (Newest to Oldest). For example, in my file the first branch, 2333, consists of rows 7-42.

    I taught myself Dynamic Named Ranges today & was able to to create the most simple macro in the world to highlight the rows for the first branch (Branch 2333) using named range "_2333":

    Please Login or Register  to view this content.
    but the next step of applying the custom sort is where I get lost. I recorded a macro where I selected the data in the named range "_2333" then applied the custom sort & this is what it returned:


    Please Login or Register  to view this content.
    The problem with that code is it's using the static ranges instead of the dynamic named range. This doesn't work considering I'm constantly adding rows to the list. When I attempted to replace all the ranges in the above code with my dynamic named range "_2333" to get this:

    Please Login or Register  to view this content.
    I get the following message: "Run Time Error '1004': The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank."

    When I hit Debug it Highlights ".Apply".

    I may even be going about this completely wrong & there might be a much better way of approaching it I just don't know enough about macros. Any help would be HUGELY appreciated.
    Attached Files Attached Files
    Last edited by dvsbyknight; 05-17-2012 at 09:07 PM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to select a data range then apply a custom sort

    I didn't test this, but try changing this line...
    .SetRange Range("_2333")

    To this...
    .SetRange Selection

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to select a data range then apply a custom sort

    Disregard my previous post. You also have to specify the columns for each .SortField

    Try this...

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    05-17-2012
    Location
    Glendale, AZ
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Macro to select a data range then apply a custom sort

    Awesome it worked perfectly! All I had to do was change the order from Ascending to Descending and it was exactly what I needed. Thank you!!!

  5. #5
    Registered User
    Join Date
    06-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Macro to select a data range then apply a custom sort

    Hi - I came across this thread while looking for a solution to my Macro situation.

    I recorded a Macro to sort based on YTD and assigned it to a button. I then recorded a Macro to sort based on YTD and assigned it to a button. These are both on the same sheet within a workbook. I realized that if a new row were added that data would not be included in the sort and that I also wouldn't be able to attempt to have this data from 5 sheets copy into my master document.

    From reading threads I gathered I could go about this a couple of ways, but a dynamic named range might be the best to allow me to copy from these workbook sheets into my master.

    I created the dynamic named range: =OFFSET('Rep Ranking'!$B$5,1,0,COUNTA('Rep Ranking'!$B$5:$B$4998)-1,58)

    I had issues editing the Macros I previously created and after coming across this thread I modified and tried the one above.
    It works pefectly for the first Macro to sort YTD, but when I copy and paste it and modify the column to sort by I receive a run time error 1004 stating application defined or object defined error and clicking debug highlights the word apply.


    Any assistance would be greatly appreicated.

    Working code:
    Please Login or Register  to view this content.
    https://www.dropbox.com/s/hweyj3txm3...nkSnapshot.png
    Last edited by arlu1201; 06-20-2013 at 01:49 AM.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to select a data range then apply a custom sort

    rng.Columns("BG:BG") is not column BG. It references column BH because rng is column B and the .Columns property acts as an offset from column B

    As a test, run the the macro below and see what column is selected.
    Please Login or Register  to view this content.
    This would define rng as column BG without using a Named range.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Macro to select a data range then apply a custom sort

    I ran the test macro and it does select BH.

    Basically not changing the column names works in the second Macro. It sorts by MTD and the YTD works as well. I am new to VBA so I am still not sure how they both sort the proper column when it is reference the same column and why the first Macro referenced the column properly. I did test it and it worked.

    The reason I used the named range was so that in my summary work book I could reference the ranking in 5 workbooks to copy and paste special. I hadn't gotten that far because of the Macro issue I was having.

    https://www.dropbox.com/s/3e04r7msda...BA%20Test.xlsm

  8. #8
    Registered User
    Join Date
    06-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Macro to select a data range then apply a custom sort

    I was incorrect. I need to change the first Macro's column to BE, not keep it the same.

    Thank you!

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to select a data range then apply a custom sort

    cfieser,

    Welcome to the forum.

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. In order to put code tags, either type [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] at the end of it, OR you can highlight your code and click the # icon at the top of your post window.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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