是否有任何VBA代码可以按工作表中的特定间隔对数据进行排序?

时间:2019-04-11 05:41:10

标签: excel vba

My data in the way it is entered

在我的数据中,我想按照“收入性质”列的升序对银行1的数据进行排序,按照“收入性质”列的升序对银行2的数据进行排序,以此类推。但是银行名称仅在银行的起始行中写入一次。可能有很多银行,每个银行中有很多数据,因此,如果我得到一个VBA代码,只需单击一下即可对数据进行排序,这将有很大的帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

代码注释说明了过程。

Option Explicit

Sub sortAreas()

    Dim a As Long

    'define the worksheet
    With Worksheets("sheet1")

        'used range in column D
        With .Range(.Cells(4, "D"), .Cells(Rows.Count, "D").End(xlUp))

            'all dates in column D
            With .SpecialCells(xlCellTypeConstants, xlNumbers)

                'loop through Areas of discontiguous range
                For a = 1 To .Areas.Count

                    'identify Area and expand to 4 columns wide
                    With .Areas(a).Resize(, 4)

                        'sort on the third column in ascending order
                        .Sort key1:=.Cells(1, 3), order1:=xlAscending, Header:=xlNo

                    End With

                Next a

            End With

        End With

    End With

End Sub