Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:09:23+00:00 2026-06-10T08:09:23+00:00

I am trying to make a multi-level sort Macro, while the amount of rows

  • 0

I am trying to make a multi-level sort Macro, while the amount of rows is dynamic. I’ve been messing around with some test data to try and get it to work, however I keep getting 1004 run-time errors from Excel. Hoping a second pair of eyes will help figure this out.

Here is the test data

A       B        C      D
Num Status  Junk    Junk
1   Open    1       1
2   Open    2       2
3   Closed  3       3
3   Open    3       3
4   Open    4       4
6   Open    6       6
8   Open    8       8
8   Open    8       8
34  Open    34      34
456 Open    456     456
5   Closed  5       5
853 Open    853     853
2345Closed  2345    2345
234 Open    234     234
23  Closed  23      23
4   Open    4       4
76  Closed  76      76
345 Open    345     345
623 Closed  623     623
523 Open    523     523
4525Closed  4525    4525
6   Open    6       6

And here is my Macro

Range("A1:D1").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add _
    Key:=Range("B1:B10"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
    "Open,Closed", DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add _
    Key:=Range("A1:A10"), SortOn:=xlSortOnValues, Order:=xlAscending,            DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Also see the Line where i have Range(“A1:A10”), want A10 to be the last row. So I was unsure what to put there.

Thanks all,

Jim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:09:25+00:00Added an answer on June 10, 2026 at 8:09 am

    Place your last row into a variable, then use that variable in your range name, like I’ve done below. I’ve also cleaned up your code a bit to make it easier to read and more efficient. You were getting Run-Time error, because you were missing the SetRange method:

    Sub mySort()
    
    Dim lngLast As Long
    lngLast = Range("A" & Rows.Count).End(xlUp).Row
    
    On Error Go to Whoa! 'in honor of Siddhart Rout (I like that :))
    With Worksheets("Sheet1").Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("B1:B" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
            "Open,Closed", DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("A1:A1" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("A1:D" & lngLast)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    On Error Go To 0
    Exit Sub
    
    Whoa!:
        Msgbox "Error " & Err.Number & " " & Err.Description, vbOkOnly
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make my gridview control in ASP.Net do a multi sort
I've been trying to make a multi-page poll with jQuery Mobile that is supposed
I'm trying to make a multi-language application with Symfony2 and I'm currently trying to
I'm trying to make my C# application multi threaded because sometimes, I get an
Trying to make the infamous checkall checkbox for dynamically created rows from a MySQL
I am trying to make a multi-dimensional array of characters in ruby, and this
I'm trying to make a multi instance engine in C++ with a wrapper for
I'm trying to make a box that allows you to select some variables, and
I am trying to make a multi module project. I have one module for
I am trying to make multi-weighted graphs. Input is to be made in following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.