2013-09-30 157 views
0

我嘗試了這對"yes"點擊並沒有做到這一點點擊"no"時想要做一個排序排序功能。我寫的代碼如下,但是當點擊是時它現在不排序。有人能幫助我並瞭解解決方案嗎?我想我犯了一個小錯誤。排序功能VBA的Excel,不排序

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 
+0

Birdsview:刪除線'如果CommandButtonSort()= 0 Then'和'否則:CommandButtonSort()= FALSE'和相應'結束Ifs' ... –

+0

我刪除了它們,但仍然無效。彈出窗口只有在我選擇「是」或「否」時結果相同。沒有排序.. – Patje

+0

股份的SortTable'()'代碼 –

回答

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