用vba对第一列进行降序,第二列进行升序,第三列进行降序
时间: 2023-12-21 12:06:57 浏览: 530
可以使用Excel VBA中的Sort方法来实现该功能。以下是示例代码:
```
Sub SortData()
Dim LastRow As Long
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row '获取最后一行
With ActiveSheet.Sort '使用Sort方法
.SortFields.Clear '清除之前的排序条件
.SortFields.Add Key:=Range("A2:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlDescending '按第一列(A列)降序排序
.SortFields.Add Key:=Range("B2:B" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending '按第二列(B列)升序排序
.SortFields.Add Key:=Range("C2:C" & LastRow), SortOn:=xlSortOnValues, Order:=xlDescending '按第三列(C列)降序排序
.SetRange Range("A1:C" & LastRow) '排序范围
.Header = xlYes '是否有标题行
.Apply '应用排序
End With
End Sub
```
注意:上述代码中假设第一行为标题行,数据从第二行开始。如果不是,请根据实际情况修改代码中的行号。
阅读全文
相关推荐


















