排序升序/降序vba excel [英] sort ascending/descending vba excel

查看:830
本文介绍了排序升序/降序vba excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对列进行排序(这是一个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)))然后
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
pre>

解决方案

此代码为我工作:

  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)))然后
xlSort = xlAscending
Else
xlSort = xlDescending
如果

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


结束
ActiveWorkbook.Save

End Sub

希望这样做的技巧!!!


I want to sort a column (it's a flagcolumn with Y/N). It should Toggle between ascending / descending on every click.

my code is not working..I am new to VBA. Any help please.

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

解决方案

This code worked for me:

  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

Hope this does the trick!!!

这篇关于排序升序/降序vba excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆