Excel VBA custom sorting sequence

Here Insert Picture Description
1 to be sorted data set
data shown in FIG. 1 concentration, if desired by the range of cells E2: E6 sequence set forth in sort AddCustomList need to use a method to add custom application sequence, the following sample code.

Sub SortByLists()
     Dim avntList As Variant, lngNum As Long
     avntList = Range("E2:E6")
     Application.AddCustomList avntList
     lngNum = Application.GetCustomListNum(avntList)
     Range("A1").Sort Key1:=Range("A1"), _
         Order1:=xlAscending, Header:=xlYes, _
         OrderCustom:=lngNum + 1
     Application.DeleteCustomList lngNum
 End Sub

Here Insert Picture Description
FIG 2 sort results

Line 4 of code of the Application object AddCustomList method to add a custom application sequence. The method of custom AddCustomList autofill (or custom order) add a custom list syntax is as follows.

AddCustomList (List Array, ByRow)

Wherein ListArray parameter is required, the object may be a string or array of Range. ByRow parameter is optional and is used only when ListArray Range object. If True, then create a custom list using each line area; If False, then each column area of ​​use create a custom list. If omitted, and the number of lines in the region more than the number of columns (or rows equal to the number of columns), each column of the Microsoft Excel using the region create a custom list. If this parameter is omitted, and the number of columns in the region than the number of rows, each row is Microsoft Excel using the area create a custom list.

If you want to add a list already exists, AddCustomList method does not work.

Line number 5 in the returned array avnList custom sequence.

Line 6 sort Sort method using the current data, wherein the parameter specifies a first Sort Key1 keyword, the default is ascending, while the set contains a title and specify a custom sequence index sorted by number of newly added.

NOTE: The parameter specifies OrderCustom integer offsets based on 1, to be defined on the basis of the serial number from 1 is added to the custom sort order list at the specified parameter.

Line 9 DeleteCustomList code uses custom serialization method to remove newly added.

Micro-channel public number: VBA168

Taobao shop address: https://item.taobao.com/item.htm?spm=a1z10.1-cs.w4004-21233576391.4.1af0683dzrx3oU&id=584940166162

Focus on micro-channel public number, in time to receive Excel VBA classic examples to explain every day.

Taobao shop to provide customized services Excel.

I wish you work and learning easier!

Published 10 original articles · won praise 3 · Views 3040

Guess you like

Origin blog.csdn.net/qq389445046/article/details/104449256