Sorting Excel中的排序公式

Sorting Excel中的排序公式,sorting,excel,excel-formula,excel-2010,vba,Sorting,Excel,Excel Formula,Excel 2010,Vba,我有如下数据: 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

我有如下数据:

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宏。有什么想法吗?

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


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


如果你有很多工作表,VBA可能是最好的选择。下面的代码是执行此操作的一种方法。它循环遍历工作簿中的所有工作表,并根据您在
SortBy1
SortBy2
中定义的变量对每个表进行排序(假设工作表只包含一个从单元格
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
选项显式
子排序和副本()
将ws设置为工作表
Dim DataRng As范围
变暗SortRng1为范围,SortRng2为范围
Dim nr为整数,nc为整数,i为整数
Dim DataArr作为变型
Dim SortBy1作为字符串,SortBy2作为字符串
将表格设置为整数
Dim HeaderFound为整数

SortBy1=“Animal”如果你有很多工作表,那么VBA可能是一个不错的选择。下面的代码是执行此操作的一种方法。它循环遍历工作簿中的所有工作表,并根据您在
SortBy1
SortBy2
中定义的变量对每个表进行排序(假设工作表只包含一个从单元格
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
选项显式
子排序和副本()
将ws设置为工作表
Dim DataRng As范围
变暗SortRng1为范围,SortRng2为范围
Dim nr为整数,nc为整数,i为整数
Dim DataArr作为变型
Dim SortBy1作为字符串,SortBy2作为字符串
将表格设置为整数
Dim HeaderFound为整数

SortBy1=“Animal”移动到谷歌工作表并简单使用

移动到谷歌工作表并简单使用

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


MOREFUNC插件

  • Morefunc插件是一个包含66个新工作表函数的免费库
  • 是一些信息(由原作者提供)
  • 这是我找到的最后一件作品
  • 这是一个好主意

获取Excel的MOREFUNC加载项并使用VSORT()


MOREFUNC插件

  • Morefunc插件是一个包含66个新工作表函数的免费库
  • 是一些信息(由原作者提供)
  • 这是我找到的最后一件作品
  • 这是一个好主意

手动/繁琐的
排序功能是什么意思?由于您只需突出显示范围并单击排序,我不确定数据集的大小为什么会影响这项工作的繁琐程度。我想你是说你想要一个更大的程序在中间没有人工干预,我的意思是,工作簿有大约一百页的自定义范围等等,并且需要几个拷贝/粘贴/分类等等。这是一个很好的论点。但是请相信我,如果我能弄清楚我所要求的功能,那真的会很有帮助吗?所有的工作表是否都有相同的列标题?您是否要求所有工作表的数据都在相同的相对位置?不一定:(@Jenny_Winters OK,明白。在VBA中使用
排序
函数当然是可行的。但这需要一些编码,特别是考虑特殊情况(例如,您提到了适用范围的移动)。如果您尝试一下,如果您遇到问题,我们会更容易提供帮助。如果需要,您可以使用Excel中的
记录宏
功能查看基本语法详细信息。手动/繁琐的
排序
功能是什么意思?因为您只需突出显示范围并单击
排序
,我不确定为什么da的大小会变大TA集影响了这是多么乏味。我想你是说你想要一个更大的程序在中间没有人工干预。我的意思是,工作簿有大约一百页的自定义范围等,并且需要几个拷贝/粘贴/分类等等。这是一个很好的观点,但是请相信我,这将是非常有用的。如果我能弄清楚我要问的功能,那么所有的工作表是否都有相同的列标题?您是在为所有工作表询问相同相对位置的数据吗?不一定:(@Jenny_Winters好的,明白了。在VBA中使用
排序
功能当然是可行的。但这需要一些编码,特别是考虑到特殊情况(例如,您提到了适用范围的移动)。如果您尝试一下,如果您遇到问题,我们会更容易帮助您。如果需要,您可以使用Excel中的
记录宏
函数查看基本语法详细信息。
B:B
列是天才!!您得到+1!@Taosique谢谢,但它是从这里偷来的;):因为vlookup对于大型数据集来说速度较慢,我会将它们更改为=INDEX(C$4:C$13,匹配(行()-行(E$3),$B$4:$B$13,0))和