寻找用于升序和降序命令按钮的宏,能够维护其他相应的数据行

时间:2017-04-11 03:25:43

标签: excel excel-vba sorting vba

将一个宏放在一起按升序和降序对列进行排序(下面发布),但我感兴趣的是一个宏,它可以按升序降序排序,但保持其他数据行与升序和降序相对应已在我的Excel工作表上移动的数据。有人知道这个场景的宏吗?

Private Sub CommandButton1_Click()


If CommandButton1.Caption = "Click to Sort Ascending" Then

'Sort ascending...
    ActiveWorkbook.Worksheets("daily data drop").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("daily data drop").Sort.SortFields.Add Key:=Range("A2:A36"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("daily data drop").Sort
        .SetRange Range("A2:A36")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
CommandButton1.Caption = "Click to Sort Decending"

Else

'sort decending
    ActiveWorkbook.Worksheets("daily data drop").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("daily data drop").Sort.SortFields.Add Key:=Range("A2:A36") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("daily data drop").Sort
        .SetRange Range("A1:A36")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

CommandButton1.Caption = "Click to Sort Ascending"

End If

End Sub

1 个答案:

答案 0 :(得分:0)

尝试更改此行

.SetRange Range("A1:A36")

到这个

.SetRange Range("A1:B36")

或更改" B"你去过哪个专栏信