将数组中存储的列的dynamic范围sorting为variables

我正在尝试编写代码,其中有人可以input数据应该sorting的列名称列表:

变量

sorting数据是位于同一张纸上的dynamic范围:

范围列

以下是我到目前为止的代码:

updateTab = Sheets("RAW_DATA_SO").Range("B8") lastRow = Sheets("RAW_DATA_SO").Range("A1048576").End(xlUp).Row Dim sortBy() As String ReDim sortBy(lastRow - 12) For rowNumber = 12 To lastRow sortBy(rowNumber - 12) = Sheets("RAW_DATA_SO").Range("A" & rowNumber) Next lastColumnAddress = Sheets("RAW_DATA_SO").Range("XFD1").End(xlToLeft).Address(False, False) serchrange = "A1:" & lastColumnAddress Set sortRange = Range(Cells(1, 10), Cells(lastRow, lastColumn)) For i = 0 To UBound(sortBy) Set FindColumn = Sheets("RAW_DATA_SO").Range(serchrange).Find(What:=sortBy(i), LookIn:=xlValues, LookAt:=xlWhole) sortByColumn = FindColumn.Address(ReferenceStyle) sortRange.Sort key1:=Range(sortByColumn), order1:=xlAscending, Header:=xlYes Next 

问题是数据只能按时sorting一列。 我怎样才能重写sorting过程,数据正在被多个列sorting? 我发现代码中可以添加更多的列,但是他们不灵活,总是有一个假设,就是现在我们通过多less列来对数据进行sorting。 我想让它成为可能只是添加sorting清单…

鉴于VBA可以同时执行一个最多三个键,似乎通过列出的sorting关键字段倒退是最好的。

 Sub dynamic_sort() Dim lc As Long, lr As Long, v As Long, k As Long, vKEYs As Variant With Sheets("RAW_DATA_SO") With .Range(.Cells(12, 1), .Cells(12, 1).End(xlDown)) vKEYs = .Value2 End With Debug.Print LBound(vKEYs, 1) & ":" & UBound(vKEYs, 1) Debug.Print LBound(vKEYs, 2) & ":" & UBound(vKEYs, 2) For k = LBound(vKEYs, 1) To UBound(vKEYs, 1) Debug.Print vKEYs(k, 1) Next k lr = .Cells(Rows.Count, 10).End(xlUp).Row lc = .Cells(1, Columns.Count).End(xlToLeft).Column - 9 With .Cells(1, 10).Resize(lr, lc) For v = UBound(vKEYs, 1) To 1 Step -3 Select Case v Case Is > 2 .Cells.Sort Key1:=.Columns(Application.Match(vKEYs(v - 2, 1), .Rows(1), 0)), Order1:=xlAscending, _ Key2:=.Columns(Application.Match(vKEYs(v - 1, 1), .Rows(1), 0)), Order2:=xlAscending, _ Key3:=.Columns(Application.Match(vKEYs(v, 1), .Rows(1), 0)), Order3:=xlAscending, _ Orientation:=xlTopToBottom, Header:=xlYes Case 2 .Cells.Sort Key1:=.Columns(Application.Match(vKEYs(v - 1, 1), .Rows(1), 0)), Order1:=xlAscending, _ Key2:=.Columns(Application.Match(vKEYs(v, 1), .Rows(1), 0)), Order2:=xlAscending, _ Orientation:=xlTopToBottom, Header:=xlYes Case 1 .Cells.Sort Key1:=.Columns(Application.Match(vKEYs(v, 1), .Rows(1), 0)), Order1:=xlAscending, _ Orientation:=xlTopToBottom, Header:=xlYes End Select Next v End With End With End Sub 

如果您有三个以上的字段进行sorting,则需要先sorting第二个字段,然后继续sorting以便向主键方向迈进。

Select Case语句提供了三个sorting选项,以便始终使用最大数量的键。

从代码和示例图像中可以find最好的结果是,“ sorting方式”列标签位于“RAW_DATA_SO”!A11中,sorting键在A12以下。 sorting范围的左上angular是“RAW_DATA_SO”!J1,sorting范围有一个标题行。

这里有一些代码我很快被打破,所以按col namesorting….但是你应该明白…

 Public Sub SortColumns(ByVal DataTable As Range, ParamArray ColumnNames() As Variant) Dim vColName As Variant Dim rSortCol As Range DataTable.Parent.Sort.SortFields.Clear For Each vColName In ColumnNames Set rSortCol = FindColumn(DataTable, vColName) If Not rSortCol Is Nothing Then _ DataTable.Parent.Sort.SortFields.Add Key:=rSortCol, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal Next With DataTable.Parent.Sort .SetRange DataTable .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub Public Function FindColumn(ByVal DataTable As Range, ByVal ColumnName) As Range Dim rPtr As Range, rHeader As Range Set rHeader = DataTable.Resize(1) Set rPtr = rHeader.Find(ColumnName, rHeader(rHeader.Count), XlFindLookIn.xlValues, XlLookAt.xlWhole) If Not rPtr Is Nothing Then Set FindColumn = rPtr.Resize(DataTable.Rows.Count) End Function