【问题标题】:VBA Code - Custom Sorts all Columns EXCEPT Date ColumnVBA 代码 - 自定义对除日期列之外的所有列进行排序
【发布时间】:2018-09-01 05:32:34
【问题描述】:

关于 SO 的第一篇文章,并且几乎不会认为自己是编码员,所以这可能很容易......我一直在努力使用这段代码,tt 似乎适用于除 F 之外的所有列,其中填充了日期格式为 mm/dd/yyyy。当代码到达带有日期的 F 列时,代码不会完全执行。

为了获取此代码,我使用了 excel 宏记录器并对范围进行了个性化设置。

Sub Sorting()
'
' Sorting Macro
'

'
    Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Ambulatory Care").Sort.SortFields.Clear
Dim lastrow As Long
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
ActiveWorkbook.Worksheets("Ambulatory Care").Sort.SortFields.Add Key:=Range( _
    "A5:A" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
ActiveWorkbook.Worksheets("Ambulatory Care").Sort.SortFields.Add Key:=Range( _
    "B5:B" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
ActiveWorkbook.Worksheets("Ambulatory Care").Sort.SortFields.Add Key:=Range( _
    "C5:C" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
ActiveWorkbook.Worksheets("Ambulatory Care").Sort.SortFields.Add Key:=Range( _
    "I5:I" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
ActiveWorkbook.Worksheets("Ambulatory Care").Sort.SortFields.Add Key:=Range( _
    "F5:F & lastrow"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
With ActiveWorkbook.Worksheets("Ambulatory Care").Sort
    .SetRange Range("A4:J798")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Range("A4").Select
End Sub

【问题讨论】:

  • ...与您已经完成的相同:.SetRange Range("A4:J" & lastrow)。作为一个额外的学习点,这个网站上有一些帖子可以帮助你在 VBA 中不使用 .SelectActiveWorkbook.Activate 这些最终会让你绊倒!

标签: excel vba date


【解决方案1】:

这是两个问题合二为一。

[It] 似乎适用于除 F 之外的所有列

您的代码有Key:=Range("F5:F & lastrow"), 而不是Key:=Range("F5:F" & lastrow),(注意" 的位置变化)。

关于如何制作 .SetRange Range("A4:J798") 动态 IE 的任何建议 A4:最后一行?

使用与之前相同的技术:.SetRange Range("A4:J" & lastrow)

一些额外的点

这里还有一些额外的学习机会。使用宏记录器是了解使用了哪些函数以及通常将哪些参数传递给这些函数的一种很好的技术。但记录器并不微妙,它记录一切,而不是开发好的代码。

如您所见,尝试定制此记录的代码可能很困难 - 宏中可能不需要记录的内容。所以我冒昧地用一些 cmets 重写了你的代码(基于记录的内容)。我没有尝试添加任何新技术 - 其他评论员可能会找出不同的有效方法来实现您想要的。

Sub Sorting()
'
' Sorting Macro
'
' You don't need the original selection lines. You are working directly with the ranges you want below.
' Good indenting is your friend - it makes the logic and flow easier to see.
' All the work you do is with the one worksheet.
' So to make life easier, I am using a single outer "With" to prevent a lot of retyping. 
    With ThisWorkbook.Worksheets("Ambulatory Care").Sort
        .SortFields.Clear
        Dim lastrow As Long
    ' make sure all your range references are fully qualified. Otherwise Excel will default to what is active at the time (which may not be what you think it is or may change during the running of the code). 
        lastrow = .Cells(.Rows.Count, 2).End(xlUp).Row
        .SortFields.Add Key:=Range( _
            "A5:A" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        .SortFields.Add Key:=Range( _
            "B5:B" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        .SortFields.Add Key:=Range( _
            "C5:C" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        .SortFields.Add Key:=Range( _
            "I5:I" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        .SortFields.Add Key:=Range( _
            "F5:F" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        .SetRange Range("A4:J" & lastrow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
' Final select not required
End Sub

现在您可以查看该代码,并确定是否可以进一步整理它。它更易于阅读和理解。

一些补充阅读:

How to avoid using Select in Excel VBA

How to avoid using .Select, .Activate, ActiveSheet,ActiveCell in my specific vba code?

Excel 2013 VBA alternative to using Activate and Select

Using Worksheet CodeName and Avoiding .Select & .Activate

在前面的问题的答案中提供的建议可以避免盲目使用录制的宏所产生的许多问题。

【讨论】:

  • @MaramMohd:请随意“接受”答案。点赞和接受答案是 StackOverflow 表达赞赏的工具。干杯,很高兴我能提供帮助。
猜你喜欢
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多