【问题标题】:sort ascending/descending vba excel升序/降序vba excel
【发布时间】:2015-04-15 23:54:00
【问题描述】:

我想对一列进行排序(它是一个带有 Y/N 的标志列)。它应该在每次点击时在升序/降序之间切换。

我的代码不起作用..我是 VBA 新手。请帮忙。

Private Sub CommandButton1_Click()

   Dim xlSort As XlSortOrder 
   Dim LastRow As Long 

   With ActiveSheet

       LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row End With

       If (Range("E2").Value > Range("E" & CStr(LastRow))) Then
           xlSort = xlAscending
       Else
           xlSort = xlDescending
       End If

       .Sort Key1:=Range("E2"), Order1:=xlSort, Header:=xlNo, _
          OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
          DataOption1:=xlSortNormal    


    ActiveWorkbook.Save  

End Sub

【问题讨论】:

  • 您缺少End With 将其放在ActiveWorkbook.Save 之前
  • 我添加了结尾。现在它显示对象不支持此属性或方法运行时错误 - 438 请建议
  • 您现在有 2 个End Withs - 删除LastRow = 行中的一个,并且在您看到Range 一词的任何地方,将其替换为.Range
  • good catch @JohnBus​​tos -> 我在编辑中没有看到这一点。代码以一种奇怪的方式被复制。
  • 仍然显示应用程序定义或对象定义运行时错误

标签: excel vba


【解决方案1】:

这段代码对我有用:

  Private Sub CommandButton1_Click()

     Dim xlSort As XlSortOrder
     Dim LastRow As Long

     With ActiveSheet

         LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row

         If (.Range("E2").Value > .Range("E" & CStr(LastRow))) Then
             xlSort = xlAscending
         Else
             xlSort = xlDescending
         End If

         .Range("E2:E" & LastRow).Sort Key1:=.Range("E2"), Order1:=xlSort, Header:=xlNo, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal


     End With
     ActiveWorkbook.Save

  End Sub

希望这能解决问题!!!

【讨论】:

  • 我完全按照您的代码进行了修改。现在它显示范围类的排序方法失败。
  • 尝试完全复制和粘贴我的代码 - 我在我的工作簿中运行了它并且它有效......
  • 成功了!!非常感谢!!
  • 我很高兴修复了它! :) - 祝你好运!
【解决方案2】:

如果您声明一个范围变量(下例中的“rng”),这将更容易。这段代码应该可以修复它。

Private Sub CommandButton1_Click()

Dim xlSort As XlSortOrder
Dim LastRow As Long
Dim rng As Range

With ActiveSheet
   LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
    Set rng = Range("E2").Resize(LastRow, 1)

    With rng
        If (.Cells(1).Value > .Cells(LastRow - 1).Value) Then
           xlSort = xlAscending
        Else
           xlSort = xlDescending
        End If

        .Sort Key1:=.Cells(1), Order1:=xlSort, Header:=xlNo, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    End With
 End With

 ActiveWorkbook.Save

End Sub

【讨论】:

  • 当我运行代码时它显示“范围类的排序方法失败”
  • 您使用的是什么版本的 Excel?
【解决方案3】:

2键升序和降序

Sub Button1_Click()

     Dim xlSort As XlSortOrder
     Dim LastRow As Long

     With ActiveSheet

         LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row

         If (.Range("E2").Value > .Range("E" & CStr(LastRow))) Then
             xlSort = xlAscending
         Else
             xlSort = xlDescending
         End If

         .Range("E2:E" & LastRow).Sort Key1:=.Range("E2"), Order1:=xlSort, Header:=xlNo, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal

     End With
     ActiveWorkbook.Save

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2010-10-13
    • 2014-10-24
    • 2014-12-11
    相关资源
    最近更新 更多