最有效的方法来排序和排序语法VBA

时间:2014-10-13 22:35:35

标签: excel excel-vba vba

我是excel的VBA宏的新手。到目前为止,该网站一直非常有用。 我有一个宏,在最后一列之后添加了四个列标题,然后在满足某个条件时填充这些列,该部分工作正常。在填充列之前,我需要对数据进行排序。我当前对数据进行排序的方法是基于记录宏,以及更改所需的变量。我已经读过,通常非常低效地记录宏。我有点坦率地说这个。以下代码有效。

Sub ineffiecientway()
Dim colltr As String
colltr = Replace(Cells(1, LastColumn).Address(True, False), "$1", "") '<-Input column index, returns column letter
Columns("A:" & colltr).Select
ActiveWorkbook.Worksheets("DSEG").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("DSEG").Sort.SortFields.Add Key:=Range("A:A") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("DSEG").Sort.SortFields.Add Key:=Range("J:J") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("DSEG").Sort
    .SetRange Range("A:" & colltr)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

下面的代码就是我一直在做的事情,并把我的头发拉出来。我敢肯定我犯了一百万个菜鸟错误。我认为这可能都是关于我失败的.sort的语法。

注意:

  • GCI()是一个用户定义的函数,它在第一行中搜索输入并返回列索引

  • LastRow()是一个用户定义的函数,它返回输入的列索引的最后一行。

  • LastColumn只返回第一行中最后使用的列

    Sub ThisDoesntWork()
    Dim ws As Worksheet
    Dim rngAll As Range
    Dim Col1 As Long 'for sort key1
    Dim Row1 As Long
    Dim Col2 As Long 'for sort key2
    Dim Row2 As Long
    Dim rng1 As Range
    Dim rng2 As Range
    Dim LastCell As Range
    
    Set ws = Worksheets("DSEG")
    Set LastCell = ws.Cells(LastRow(LastColumn), LastColumn)
    Col1 = GCI("CDate")
    Row1 = LastRow(Col1)
    Col2 = GCI("Start Time")
    Row2 = LastRow(Col2)
    
    
    Set rngAll = ws.Range(ws.Cells(1, 1), LastCell)
    Set rng1 = ws.Range(ws.Cells(1, Col1), ws.Cells(Row1, Col1))
    Set rng2 = ws.Range(ws.Cells(1, Col2), ws.Cells(Row2, Col2))
    
    
    MsgBox rng1.Address
    MsgBox rng2.Address
    MsgBox rngAll.Address
    
    With rngAll
    .Sort key1:=Range(rng1), order1:=xlAscending, DataOption1:=xlSortNormal, _
    key2:=.Range(rng2), order2:=xlAscending, DataOption2:=xlSortTextAsNumbers, _
    Header:=xlYes
    End With
    

当我运行此代码时,它在“.sort”处停止并显示错误“运行时错误'1004':对象'_Global'的方法”范围'失败 我也试过“DataOption1:= xlSortNormal”,因为我不相信第一个范围需要将数据排序为数字,这两个都会导致相同的错误。 我正在尝试上面的代码而没有设置范围或“Dim”工作表,并认为在运行代码之前设置范围会有所帮助。 我为范围添加了MsgBox以确保它们是我想要的范围。

  • 首先,MsgBox返回$ A $ 1:$ A $ 38061

  • 第二个MsgBox返回$ J $ 1:$ J $ 38061

  • 第三个MsgBox返回$ A $ 1:$ S $ 38061

前两个是我想要排序的范围,最后一个是我要排序的所有数据的范围,这些是正确的范围。

非常感谢任何有关此工作的建议或帮助。此外,任何关于更好发布的建议,因为我确定在“正确的发布格式”上也犯了错误。

编辑:感谢Nanashi,我不会重复功能,我很感激提示 谢谢Jeeped。当前区域位清理了很多。并且是.columns修复了错误(我正在尝试。范围)百万感谢你们两个。工作代码如下。

Dim ws As Worksheet
Dim Col1 As Long 
Dim Col2 As Long 
Set ws = Worksheets("DSEG")
Col1 = GCI("CDate") 'searches string and returns column index
Col2 = GCI("Start Time") 'searches string and returns column index

With ws.Cells(1, 1).CurrentRegion.Cells
.Sort key1:=.Columns(Col1), order1:=xlAscending, DataOption1:=xlSortNormal, _
      key2:=.Columns(Col2), order2:=xlAscending, DataOption2:=xlSortTextAsNumbers, Header:=xlYes
End With

1 个答案:

答案 0 :(得分:0)

通常,我.Sort的任何区域都没有任何完全空白的行或列来分解.CurrentRegion,因此我使用它来定义要排序的范围。 .Cells(1,1).CurrentRegion相当于选择A1并点击 Ctrl + A 。它包含从A1扩展的数据的 island ,直到它到达右边的完全空白列或完全空白的行。

Sub prettyefficient()
    With Sheets("DSEG").Cells(1, 1).CurrentRegion.Cells
        .Sort Key1:=.Columns(1), Order1:=xlAscending, DataOption1:=xlSortTextAsNumbers, _
              Key2:=.Columns(10), Order2:=xlAscending, DataOption2:=xlSortTextAsNumbers, _
              Orientation:=xlTopToBottom, Header:=xlYes
    End With
End Sub

您可以在一个命令中使用最多三个键( Key1 Key2 &amp; Key3 )。如果您需要更多,请将其分为两个命令。