排序函数VBA Excel,不';t排序
我正在尝试一个排序功能,它必须在单击“是”时进行排序,而在单击“否”时不进行排序。下面是我写的代码,但是当单击Yes时它现在没有排序。有人能帮助我并知道解决方法吗?我想我犯了一个小错误排序函数VBA Excel,不';t排序,vba,sorting,excel,Vba,Sorting,Excel,我正在尝试一个排序功能,它必须在单击“是”时进行排序,而在单击“否”时不进行排序。下面是我写的代码,但是当单击Yes时它现在没有排序。有人能帮助我并知道解决方法吗?我想我犯了一个小错误 Private Sub CommandButtonSort_Click() 'The sort function causes shifts in addresses and 'forces a complete consistency check and complete PLC download 'whic
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
分类表:
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
Sub-SortTable(可选ByVal sKey1为String=“”、可选ByVal sKey2为String=“”、可选ByVal sKey3为String=“”、可选ByVal sTable为String=“”)
Dim vOrder1作为变体
Dim vOrder2作为变体
Dim vOrder3作为变型
昏暗的灯光如XLYES所示
vOrder1=xl
vOrder2=xl
vOrder3=xl
如果稳定=“那么
sTable=“打印区域”
clsHeader=xlYes
其他的
clsHeader=xlNo
如果结束
sKey2=IIf(sKey1=sKey2,“,sKey2)
sKey3=IIf(sKey1=sKey3,“,sKey3)
sKey3=IIf(sKey2=sKey3,“,sKey3)
如果sKey2=“”,则
sKey2=sKey3
sKey3=“”
如果结束
如果是“1”,那么
活动表。取消保护
Application.GoTo引用:=稳定
如果左(sKey1,1)=“-”,则
vOrder1=xl
sKey1=Mid(sKey1,2)
如果结束
如果是sKey2“,那么
如果左(sKey2,1)=“-”,则
vOrder2=xl
sKey2=Mid(sKey2,2)
如果结束
如果sKey3“那么
如果左(sKey3,1)=“-”,则
vOrder3=xl
sKey3=Mid(sKey3,2)
如果结束
选择。排序_
Key1:=Range(sKey1),Order1:=vOrder1,Key2:=Range(sKey2),Order2:=vOrder2,Key3:=Range(sKey3),Order3:=vOrder3_
标题:=clsHeader,订单定制:=1,匹配案例:=False,方向:=xlTopToBottom_
数据选项1:=xlSortNormal,数据选项2:=xlSortNormal
其他的
选择。排序_
Key1:=范围(sKey1),Order1:=vOrder1,Key2:=范围(sKey2),Order2:=vOrder2_
标题:=clsHeader,订单定制:=1,匹配案例:=False,方向:=xlTopToBottom_
数据选项1:=xlSortNormal,数据选项2:=xlSortNormal
如果结束
其他的
选择。排序_
键1:=范围(sKey1),顺序1:=vOrder1_
标题:=clsHeader,订单定制:=1,匹配案例:=False,方向:=xlTopToBottom_
数据选项1:=xlSortNormal,数据选项2:=xlSortNormal
如果结束
范围(sKey1)。选择
ActiveSheet.Protect DrawingObject:=False,Content:=True,Scenarios:=True,AllowFiltering:=True
如果结束
端接头
Birdsview:删除行如果CommandButtonSort()=0,那么
和否则:CommandButtonSort()=False
和相应的End If
…我删除了它们,但仍然不起作用。只有当我选择“是”或“否”时,弹出窗口才会工作,结果相同。无排序..共享SortTable()
code如何将代码添加到注释中?在注释中读取代码很困难。你能用代码更新你的问题吗?
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