Excel SortFields 添加然后排序 [英] Excel SortFields add then sort

查看:33
本文介绍了Excel SortFields 添加然后排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我理解这个片段吗:

Would you help me understand this snippset:

首先好像是用

MainSheet.Sort.SortFields.Clear
For lI = 1 To vSortKeys(0, 1)   
    MainSheet.Sort.SortFields.Add Key:=Range(vSortKeys(lI, 1) & 2), 
       SortOn:=xlSortOnValues, Order:=vSortKeys(lI, 2), DataOption:=xlSortNormal
Next

然后,我了解到以下代码正在有效地运行排序

Then, I understand that the following code is effectively running the sort

With MainSheet.Sort
    .SetRange Range("A" & lFrom & ":" & GEN_REV_END & lTo)
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

这种解释是否正确 - 需要先添加排序规则,然后将其与第二部分一起应用?

Is this interpretation correct - need to add a sorting rule first, then apply it with the second part ?

那么,我们为什么要在第二部分中定义排序范围,在

Then, why are we defining a range for sort in the second part, in

With MainSheet.Sort
    .SetRange Range("A" & lFrom & ":" & GEN_REV_END & lTo)

End With

我们为 Key:=Range(vSortKeys(lI, 1) & 2) 排序的规则中还没有吗?排序在哪个单元格范围内有效运行?

Is it not already in the rule that we sort for Key:=Range(vSortKeys(lI, 1) & 2) ? On which range of cells is the sort effectively run ?

推荐答案

排序正在应用于 Sort.SetRange 中指定的范围.Sort.SortFields.Add 中的Key 参数允许您指定将确定排序顺序的字段.每个字段可以只是具有列标题的单元格.您可以为多个排序级别添加多个键.

The sorting is being applied to the range specified in Sort.SetRange. The Key parameter in Sort.SortFields.Add allows you to specify fields that will determine the order of soring. Each field can be just the cell with the column header. You can add multiple keys for several sorting levels.

举个例子,如果你在单元格A1:C10中有数据,你想按升序排序,以A列的信息为key对于排序,您可以这样做以将列 A 中的数据设置为键:

To give an example, if you have data in cells A1:C10 and you want to sort it in an ascending manner, taking information in column A as the key for sorting, you can do this to set data in column A as the key:

MainSheet.Sort.SortFields.Add Key:=Range("A1") '("A1:A10") will also work

然后您可以指定将根据该键排序的范围,如下所示:

And then you can specify the range that will be sorted based on that key as follows:

 MainSheet.Sort.SetRange Range("A1:C10")

这篇关于Excel SortFields 添加然后排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆