【问题标题】:Sorting formula in ExcelExcel中的排序公式
【发布时间】:2014-11-21 12:18:00
【问题描述】:

我有这样的数据:

bear        94
cat         25
alligator   53
impala      55
elk         56
fox         47
dog         13
gecko       18
jaguar      32
hound       59

...但我想在同一张 Excel 工作表上拥有该表的两个“副本”,第一个排序在第一列上,例如:

alligator   53
bear        94
cat         25
dog         13
elk         56
fox         47
gecko       18
hound       59
impala      55
jaguar      32

...第二个表将再次是相同的数据,但在第二列上排序,如下所示:

bear        94
hound       59
elk         56
impala      55
alligator   53
fox         47
jaguar      32
cat         25
gecko       18
dog         13

...但问题是我希望必须在 excel 中使用实际的“排序”功能!这听起来可能很疯狂,但我有一个更大的应用程序,手动排序会非常乏味。如果可能的话,我想要一个自动执行此操作的公式,但我也可以使用 excel-VBA 宏。有什么想法吗?

【问题讨论】:

  • sort 功能手动/乏味是什么意思?由于您只需突出显示范围并单击sort,我不确定为什么数据集的大小会影响这将是多么乏味。我猜你是说你想要一个更大的程序在中间没有人工干预的情况下工作?
  • 我的意思是该工作簿有大约一百张带有自定义范围等的工作表,并且需要多次复制/粘贴/排序等。这是一个很好的观点,但请相信我如果我能弄清楚我要求的功能会非常有帮助
  • 所有工作表都有相同的列标题吗?
  • 您是在询问所有工作表的相同相对位置的数据吗?不一定:(
  • @Jenny_Winters 好的,明白了。在 VBA 中使用SORT 函数当然是可行的。但这需要一些编码,特别是考虑特殊情况(例如,您提到适用范围移动)。如果您尝试一下,如果您遇到困难,我们会更容易提供帮助。如果需要,您可以使用 Excel 中的record macro 函数查看基本语法详细信息。

标签: sorting excel excel-formula excel-2010 vba


【解决方案1】:

好的,这是我想出的解决方案。也许有更优雅的方式,请告诉我!谢谢大家:)

【讨论】:

  • B:B 专栏是天才!!你得到 +1!
  • @Taosique 谢谢,但它是从这里偷来的;):chandoo.org/wp/2008/10/22/sorting-text-in-excel-using-formulas
  • 因为 vlookup 对于大型数据集来说很慢,我会将它们更改为 =INDEX(C$4:C$13,MATCH(ROW()-ROW(E$3),$B$4:$B$13 ,0)) 和 =INDEX(D$4:D$13,MATCH(ROW()-ROW(F$3),$B$4:$B$13,0))。如果有两个或多个具有相同值的“动物”,那么值排序结果也会出现问题。它只会返回名字。
  • 解决重复数据问题:A列=VALUE(D4&"."&ROW()) G列=INDEX($C$4:$C$13,MATCH(LARGE($A$4:$ A$13,ROW()-ROW(I$3)),$A$4:$A$13,0))
【解决方案2】:

如果您有很多工作表,VBA 可能是要走的路。以下代码是执行此操作的一种方法。它遍历工作簿中的所有工作表,并根据您在SortBy1 和@987654323 中定义的变量对每个表进行排序(假设工作表仅包含从单元格A1 开始的一个表) @。

它将按SortBy2 对表格进行排序,将其复制到原始表格下方,然后再次按SortBy1 对原始表格进行排序。只要您要排序的变量在整个工作簿中都命名相同,这应该可以工作。

Option Explicit

Sub SortAndCopy()
    Dim ws As Worksheet
    Dim DataRng As Range
    Dim SortRng1 As Range, SortRng2 As Range
    Dim nr As Integer, nc As Integer, i As Integer
    Dim DataArr As Variant
    Dim SortBy1 As String, SortBy2 As String
    Dim nBelowTable As Integer
    Dim HeaderFound As Integer

    SortBy1 = "Animal"  '<~~ Define the first variable to sort by
    SortBy2 = "Count"   '<~~ Define the second variable to sort by
    nBelowTable = 5     '<~~ Defines how far below the original table you want to place a copy

    Application.ScreenUpdating = False

    'Loops through each individual sheets
    For Each ws In ActiveWorkbook.Sheets
        HeaderFound = 0
        'Determines data range
        nr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        nc = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        Set DataRng = ws.Range("A1:" & ws.Cells(nr, nc).Address)

        'Determines ranges to sort by
        For i = 1 To nc Step 1
            If LCase(ws.Cells(1, i).Value) = LCase(SortBy1) Then
                Set SortRng1 = ws.Range(ws.Cells(1, i).Address & ":" & ws.Cells(nr, i).Address)
                HeaderFound = HeaderFound + 1
            End If
            If LCase(ws.Cells(1, i).Value) = LCase(SortBy2) Then
                Set SortRng2 = ws.Range(ws.Cells(1, i).Address & ":" & ws.Cells(nr, i).Address)
                HeaderFound = HeaderFound + 1
            End If
        Next i

        'Exit if header not found
        If Not HeaderFound = 2 Then
            MsgBox "One of the header variables could not be found in the sheet " & ws.Name & ". No further sheets will be processed!", vbCritical
            Exit Sub
        End If

        'Sorts table by SortBy2
        With ws.Sort.SortFields
            .Clear
            .Add Key:=SortRng2, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        End With
        With ws.Sort
            .SetRange DataRng
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

        'Places copy of this table underneath the original
        ReDim DataArr(1 To nr, 1 To nc)
        DataArr = DataRng
        ws.Range(ws.Cells(nr + nBelowTable, 1).Address, ws.Cells(2 * nr + nBelowTable - 1, nc).Address) = DataArr

        'Sorts table by SortBy1
        With ws.Sort.SortFields
            .Clear
            .Add Key:=SortRng1, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        End With
        With ws.Sort
            .SetRange DataRng
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

    Next ws

    Application.ScreenUpdating = False
End Sub

【讨论】:

  • 非常强大,但对于这个应用程序来说可能有点矫枉过正
【解决方案3】:

获取 Excel 的 MOREFUNC 插件并使用 VSORT()


更多功能插件

【讨论】:

    【解决方案4】:

    移动到 Google 工作表并简单地使用 SORT()

    【讨论】:

    • ...或者在 .NET 中编写一个控制台应用程序,该应用程序将通过 ACE OleDB 打开文件并对其进行排序。问题与 Google 表格无关。
    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 2017-12-03
    • 1970-01-01
    • 2015-09-29
    • 2015-03-17
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多