VBA difference in Sort with: Add2 and Add (.SortFields.Add2 VS .SortFields.Add)

jbesclapez

Active Member
Joined
Feb 6, 2010
Messages
275
Hello,

I sent a macro to another computer only to realize that it did not work because of a Sort issue in the code.
HTML:
    With ActiveWorkbook.Worksheets("APP").Sort    .SortFields.Clear    .SortFields.Add2 Key:=Range("B2:B" & LastA), Order:=xlAscending    .SortFields.Add2 Key:=Range("A2:A" & LastA), Order:=xlAscending    .SortFields.Add2 Key:=Range("C2:C" & LastA), Order:=xlAscending    .SortFields.Add2 Key:=Range("D2:D" & LastA), Order:=xlAscending    .SortFields.Add2 Key:=Range("E2:E" & LastA), Order:=xlAscending    .SortFields.Add2 Key:=Range("F2:F" & LastA), Order:=xlAscending                .SetRange Range("A1:G" & LastA)        .Header = xlYes        .Apply            End With

The macro above is not working on Office365 Excel but is working in Excel 2016.
When I replace in this macro the .SortFields.Add2 by .SortFields.Add then it is working!

So I decided to change in all my project the .SortFields.Add2 by .SortFields.Add
However, even if the macro is still working, the result shown is different.

So, Which one should I use and when? Also what is the difference then?

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The Add2 method was added in 2016 (I can only guess that your 365 version is out of date).
 
Upvote 0
The Add2 method was added in 2016 (I can only guess that your 365 version is out of date).


Hi Rory, hope you are fine and thanks for your reply.

I already guessed that it was a new method but I still cannot make any sense out of it.
Here is Microsoft site : https://docs.microsoft.com/en-us/office/vba/api/excel.sortfields.add
But even with the description i dont get it.

Add: [FONT=&quot]Creates a new sort field and returns a [/FONT][FONT=&quot]SortFields[/FONT][FONT=&quot] object.
Add2: [/FONT]
[FONT=&quot]Creates a new sort field and returns a [/FONT][FONT=&quot]SortFields[/FONT][FONT=&quot] object that can optionally sort data types with the SubField defined.

Any idea of what they mean by that?[/FONT]
 
Upvote 0
The latest versions of Excel (only in 365) have new data types added (Geography and Stock for example currently) that you can sort by using the new method.
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,315
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top