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

  • Home
  • SEARCH
  • 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 6325685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:56:31+00:00 2026-05-24T16:56:31+00:00

There are essentially 2 questions: For excel’s .sort function: How do I add keys

  • 0

There are essentially 2 questions:

For excel’s .sort function: How do I add keys when I do not know how many keys (headers) a given sheet is going to have? (See Below)

I want to pass a number of predefined arrays, containing information on how to sort a given selection, to excel’s .sort algorithm… How do I do this correctly?

Problem Description:

I have a sheet that lists headers in the first row. The headers are different names and there can be a variable number of headers present in the first row. The headers describe different details of metrics listed in the rows below on the sheet (one metric/row).

I need to write a code such that I can sort certain bound-together rows, not the entire sheet. For instance, I might need to sort the 28th-35th row. This will be passed to this program by the variable inRange. However, I also want to be able to configure the sort priority and how each column gets sorted everytime I run the macro. (note that a row can only change position as a whole, individual cells within the row cannot change position)

I have the following Global arrays which describe the headers:

headRow – contains an array of the header names as strings listed in order of the column

prLst – contains an array of integers (input as strings) that determine priority of each of the headers. The position of the integers in the array is the column number which they describe.

colIsString – contains Boolean determining if items listed in a given column are strings or integers. True = string, False = Integer. Again, position position of booleans in the array is the column number which they describe.

sortOrder – containing booleans specifying orientation of sort. “True” – ascending. “False” – descending. Yet again, position position of booleans in the array is the column number which they describe.***

With the data in the arrays already etablished, I have the following code to feed these arrays to .sort:

Dim numHdrs, finalNumHdrs, count As Integer, newArray As Variant
For numHdrs = 1 To UBound(headRow)
    If colIsString(numHdrs) = True Then
        sortOrder(numHdrs) = xlAscending
    ElseIf colIsString(numHdrs) = False Then
        sortOrder(numHdrs) = xlDescending
    End If
Next numHdrs

newArray = CombineArrays(headRow, prLst, sortOrder)

For finalNumHdrs = 1 To UBound(headRow)
    If headRow(finalNumHdrs) And prLst(finalNumHdrs) And sortOrder(finalNumHdrs) <> "N/A" Then
        'ActiveSheet.Sort.SortFields.Add Key(headRow(finalNumHdrs)):= finalNumHdrs
        'ActiveSheet.Sort.SortFields.Add Order(finalNumHdrs):=sortOrder(finalNumHdrs)
    End If
Next finalNumHdrs

With ActiveSheet.Sort
    .SetRange inRange
    .Apply
End With

I am having trouble adding the sort fields appropriately, using the data I have in the array:

'ActiveSheet.Sort.SortFields.Add Key(headRow(finalNumHdrs)):= finalNumHdrs
'ActiveSheet.Sort.SortFields.Add Order(finalNumHdrs):=sortOrder(finalNumHdrs)

This is obviously not correct. So I created a function to concatenate headRow, prLst, and sortOrder to make it easier to feed into .sort:

Function CombineArrays(arr1 As Variant, arr2 As Variant, arr3 As Variant)
Dim arr4 As Variant
ReDim arr4(1 To UBound(arr2), 1 To 2)

Dim i, j As Integer
For i = 0 To UBound(arr1)
    arr4(arr2(i), 1) = arr1(i)
    arr4(arr2(i), 2) = arr3(i)
Next i
CombineArrays = arr4
End Function

Since arr2 is specifying the sort priority of a given column, I need to feed it to .sort in such a way that arr2 specifies the key number. As in if arr2 = 3, key3:= arr4(3,1), order3:= arr4(3,2). I am assuming that the order in which I elements to arr4 will not matter. (as in if I add arr4(4,1) before arr4(3,1) they will still be listed in the appropriate order)

This boils down to two questions:

For excel’s .sort function: How do I add keys when I do not know how many keys (headers) a given sheet is going to have?

Am I doing this correctly?

*** Note I would have used collection objects had I heard about them earlier. However, since I am new to VBA, this is how I established the program. It would be too difficult given the code I have already written to go back and change all the arrays to collection objects.

NOTE: This is part of a larger program, whose description can be found here: Sorting Groups of Rows Excel VBA Macro

  • 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-05-24T16:56:33+00:00Added an answer on May 24, 2026 at 4:56 pm

    I think you expect the sort method to accept an array of sorting keys and orders which it can’t do. The sort is limited to a max of 3 sorting columns (just like it is in the User interface)

    You will need to make use of the trick that you can end up with the same sorting results by doing it column by column from the least important one to most important one. The next code is incomplete and won’t exactly be what you need but is intended to explain the general idea

    ' Assuming you have some array which lists the order in which to sort from most important to least important named priorityArr 
    For i = UBound(priortyArr) To LBound(prioryArr)
      ActiveSheet.Sort.SortFields.Add Key:= headRow(i) Order:=sortOrder(i)
      ' do the rest you need to do and apply the sort
    Next
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I know there have been multiple questions regarding Money and converting to and
I know there are loads of questions on replacewith but none seem to have
There are questions here how to get a Map s keys associated with a
I know there are a few questions regarding the libraries you can use to
There are essentially 2 places to define JavaScript functions in Grails, directly in a
I have a mockup layout for something here . Essentially there are sections, columns
Surely there is some kind of abstraction that allows for this? This is essentially
If I want to essentially grep every line ever in the repository, is there
So I have what is essentially a spreadsheet in TIFF format. There is some
I am working with a set of what is essentially Attribute/Value pairs (there's actually

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.