How to Fix Excel VBA Named Argument Not Found Error in Range.Sort
Question details
The user needs to sort data using four or more keys via VBA, but encounters a compiler error because the Range.Sort method only accepts up to three keys.

- Product
- Excel VBA
- Device & OS
- not provided
- Scenario
- Attempting to automate a multi-column data sort using more than three keys (e.g., Key4, Key5) within a single Range.Sort macro statement.
- Observed behavior
- The VBA compiler throws a 'Named argument not found' error because the Range.Sort method only supports Key1, Key2, and Key3.
Before modifying your VBA script, clarify the exact priority order of the columns you want to sort, ranking them from your most important criteria to your least important.
Apply Multiple Sorts in Reverse Priority Order
Since Range.Sort only allows three keys per operation, you can achieve the desired result by running consecutive sorts starting from your least important keys up to the most important ones.
Excel's legacy Range.Sort method strictly limits sorting to a maximum of three keys (Key1, Key2, Key3). Attempting to pass a Key4 or higher argument will always trigger the 'Named argument not found' error.
To bypass this limitation, you can take advantage of stable sorting. By sorting your lowest-priority columns first, their relative order is maintained when you subsequently sort by the higher-priority columns.
List out all the columns you want to sort by and rank them from highest priority (1) to lowest priority (e.g., 7).
Write your first Range.Sort statement targeting your least important keys. For example, assign your 5th, 6th, and 7th priority columns to Key1, Key2, and Key3 in this initial sort command.
Write the next Range.Sort statement for the next tier of priorities, assigning the 4th priority column to Key1. Since it's only one key, you omit Key2 and Key3.
Write the final Range.Sort statement for your highest priority columns. Assign priority 1, 2, and 3 to Key1, Key2, and Key3 respectively.
Run the VBA script. The macro will sort the data sequentially, resulting in the correct final order just as if you had sorted by all 7 keys simultaneously.

Execute Advanced VBA Macros with WPS Spreadsheet
WPS Office provides excellent VBA compatibility. You can easily write, edit, and run complex multi-level sorting macros smoothly within WPS Spreadsheet.
- 1. Open the VBA Editor: Open your workbook in WPS Spreadsheet, navigate to the Developer tab, and click the VBA Editor icon (or press Alt + F11).
- 2. Insert the reverse-priority script: Right-click your project, select Insert > Module, and paste your reverse-priority Range.Sort macro code.
- 3. Run the multi-level sort: Click the Run button (or press F5) to execute the consecutive sorts, flawlessly arranging your data.

Frequently Asked Questions
Why does Range.Sort only support 3 keys in VBA?
Historically, the VBA object model for Range.Sort was designed to mirror the older interface limitations of early spreadsheet software, which only allowed sorting by three columns at a time. This legacy limitation remains intact for backward compatibility.
Is there an alternative to Range.Sort for more than 3 keys?
Yes. If your environment supports it, you can use the newer Worksheet.Sort object model. This approach allows you to use the SortFields.Add method to sequentially add as many sorting fields as needed without hitting the 3-key limit.
Does the reverse sorting method work in WPS Spreadsheet?
Absolutely. WPS Spreadsheet's VBA environment is highly compatible with standard macro syntax. You can run the exact same multi-step Range.Sort macros to achieve your desired data order.
What is a stable sort?
A stable sort maintains the original relative order of records that have equal sort keys. In spreadsheet automation, this means if you sort by a less important column first and then by a more important column, the ties in the second sort are resolved by the results of the first sort.




