排序功能VBA Excel,不排序

时间:2013-09-30 10:44:50

标签: excel vba sorting excel-vba

我正在尝试排序功能,该功能必须在点击"yes"时进行排序,而不是在点击"no"时执行。我写的代码如下,但是点击“是”时它现在没有排序。有人可以帮助我并知道解决方案吗?我想我犯了一个小错误。

Private Sub CommandButtonSort_Click()

'The sort function causes shifts in addresses and
'forces a complete consistency check and complete PLC download
'which causes a lot of downtime and risks of parameters and settings which have been defaulted.'
If CommandButtonSort() = 0 Then
    MsgBox "Are you absolutely sure? A complete consistency check and plc download is forced", vbInformation + vbYesNo, "Sort function"
    If vbYesNo = Yes Then
    Dim sKey1 As String
    Dim sKey2 As String
    Dim sKey3 As String

    sKey1 = Worksheets("Setup").Range("lblTagSortByKey1").Value
    sKey2 = Worksheets("Setup").Range("lblTagSortByKey2").Value
    sKey3 = Worksheets("Setup").Range("lblTagSortByKey3").Value
    Call SortTable(sKey1, sKey2, sKey3, "tblTags_All")
    Else: CommandButtonSort() = False
    End If
    End If

Sorttable:

Sub SortTable(Optional ByVal sKey1 As String = "", Optional ByVal sKey2 As String = "", Optional ByVal sKey3 As String = "", Optional ByVal sTable As String = "")

    Dim vOrder1 As Variant
    Dim vOrder2 As Variant
    Dim vOrder3 As Variant
    Dim clsHeader As XlYesNoGuess

    vOrder1 = xlAscending
    vOrder2 = xlAscending
    vOrder3 = xlAscending

    If sTable = "" Then
        sTable = "Print_Area"
        clsHeader = xlYes
    Else
        clsHeader = xlNo
    End If

    sKey2 = IIf(sKey1 = sKey2, "", sKey2)
    sKey3 = IIf(sKey1 = sKey3, "", sKey3)
    sKey3 = IIf(sKey2 = sKey3, "", sKey3)
    If sKey2 = "" Then
        sKey2 = sKey3
        sKey3 = ""
    End If

    If sKey1 <> "" Then
        ActiveSheet.Unprotect
        Application.GoTo Reference:=sTable
        If Left(sKey1, 1) = "-" Then
            vOrder1 = xlDescending
            sKey1 = Mid(sKey1, 2)
        End If
        If sKey2 <> "" Then
            If Left(sKey2, 1) = "-" Then
                vOrder2 = xlDescending
                sKey2 = Mid(sKey2, 2)
            End If
            If sKey3 <> "" Then
                If Left(sKey3, 1) = "-" Then
                    vOrder3 = xlDescending
                    sKey3 = Mid(sKey3, 2)
                End If
                Selection.Sort _
                    Key1:=Range(sKey1), Order1:=vOrder1, Key2:=Range(sKey2), Order2:=vOrder2, Key3:=Range(sKey3), Order3:=vOrder3, _
                    Header:=clsHeader, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                    DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
            Else
                Selection.Sort _
                    Key1:=Range(sKey1), Order1:=vOrder1, Key2:=Range(sKey2), Order2:=vOrder2, _
                    Header:=clsHeader, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                    DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
            End If
        Else
            Selection.Sort _
                Key1:=Range(sKey1), Order1:=vOrder1, _
                Header:=clsHeader, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
        End If

        Range(sKey1).Select
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFiltering:=True
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub CommandButtonSort_Click()

    If MsgBox("Are you absolutely sure? A complete consistency check and plc download is forced", vbInformation + vbYesNo, "Sort function") = vbYes Then
    'If vbYesNo = Yes Then'
    Dim sKey1 As String
    Dim sKey2 As String
    Dim sKey3 As String

    sKey1 = Worksheets("Setup").Range("lblTagSortByKey1").Value
    sKey2 = Worksheets("Setup").Range("lblTagSortByKey2").Value
    sKey3 = Worksheets("Setup").Range("lblTagSortByKey3").Value
    Call SortTable(sKey1, sKey2, sKey3, "tblTags_All")
    End If



End Sub