Exce使用VBA对多于4列的数据进行组合排序

Sub Sort() ' ' Sort Macro ' 宏由 zhengzhe 录制,时间: 2010-4-13 ' Dim workArea As Range ' 定义工作区域 Dim LastColumn As Long ' 最后一列 Dim LastRow As Long ' 最后一行 Dim sumRange As Range ' 汇总行区域 Application.ReferenceStyle = xlR1C1 With ActiveSheet LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With Set workArea = Range(Cells(1, 1), Cells(LastRow, LastColumn)) workArea.Select '使用倒排序方式对多列进行排序 For col = LastColumn To 2 Step -1 workArea.Sort Key1:=Cells(1, col), Order1:=xlAscending Next col sumRow = LastRow + 1 sumCol = LastColumn + 1 Set sumRange = Range(Cells(sumRow, 1), Cells(sumRow, LastColumn)) With sumRange .Cells(1, 1).Value = "总计" .Cells(1, 1).Offset(0, 1).Activate ActiveCell.FormulaR1C1 = "=COUNT(R2C:R[-1]C)" Selection.AutoFill Destination:=Range(Cells(sumRow, 2), Cells(sumRow, LastColumn)), Type:=xlFillDefault .Font.Bold = True .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter With .Interior .ColorIndex = 15 .Pattern = xlSolid End With End With End Sub

你可能感兴趣的:(VBA)