对语法VBA进行sorting和sorting的最有效的方法

我对excel的VBAmacros很新。 这个网站迄今为止非常有帮助。 我有一个macros在最后一列之后添加四个列标题,然后填充这些列,如果满足某个标准,那部分工作正常。 在填充列之前,我需要对数据进行sorting。 我目前的数据sorting方法是基于logging一个macros,并改变所需的variables。 我已经读过,通常excel非常低效地loggingmacros。 我有一种坦率的认同。 以下代码工作。

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()是用户定义的函数,用于在第一行中searchinput并返回列索引

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

  • 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 

当我运行这段代码时,它停在错误“运行时错误1004”的“.sort”:object'_Global的方法“范围”失败我也尝试了“DataOption1:= xlSortNormal”,因为我不'相信第一个范围需要将文本sorting为数字,都会导致相同的错误。 我正在尝试上面的代码,没有设置范围或“昏暗”的工作表,并认为在运行代码之前设置范围将有所帮助。 我为范围添加了MsgBox,以确保它们是我想要的范围。

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

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

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

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

任何意见或帮助得到这个工作将不胜感激。 此外,任何build议更好的张贴,因为我敢肯定,“适当的发布格式”的错误也是如此。

编辑:谢谢Nanashi,我不会重复function,我欣赏提示。
感谢Jeeped。 目前的地区位清理了很多。 这是固定错误的列(我正在尝试。范围)百万感谢你们俩。 工作代码如下。

 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 

通常情况下,任何区域我.Sort没有任何完全空白的行或列分解.CurrentRegion所以我用它来定义范围进行sorting。 .Cells(1,1).CurrentRegion相当于selectA1并点击Ctrl + A。 它包含从A1扩展的数据 ,直到它到达完全空白的列到右侧或完全空白的行。

 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 

一个命令最多可以使用三个键( Key1Key2Key3 )。 如果你需要更多的话,把它分成两个命令。