简体   繁体   English

根据范围内的数据组织/分类工作簿表

[英]Organize/sort workbook sheets according to data in a range

I need to sort some worksheets in Excel 2010 according to a specific rule. 我需要根据特定规则对Excel 2010中的一些工作表进行排序。 In the workbook where the macro runs (Workbook_0) I have a list of words, all in one row, one per column, for example 在运行宏的工作簿中(Workbook_0),我有一个单词列表,它们全部排成一行,例如,每列一个

  • A:"mango" B:"apple" C:"banana". A:“芒果” B:“苹果” C:“香蕉”。 I save this range in a variable of type Range. 我将此范围保存在Range类型的变量中。

My macro creates a new workbook (Workbook_1) with sheets called, for example, 我的宏创建了一个新的工作簿(Workbook_1),其中包含工作表,例如,

  • "apple_count", "apple_avg", "banana_count", "banana_average", "mango_count", "mango_avg". “ apple_count”,“ apple_avg”,“ banana_count”,“ banana_average”,“ mango_count”,“ mango_avg”。

What I would like to do is to sort the sheets in Workbook_1 according to the order specified in Workbook_0. 我想做的是根据Workbook_0中指定的顺序对Workbook_1中的工作表进行排序。 In the example, I would obtain 在示例中,我将获得

  • "mango_count", "mango_avg", "apple_count", "apple_avg", "banana_count", "banana_avg". “ mango_count”,“ mango_avg”,“ apple_count”,“ apple_avg”,“ banana_count”,“ banana_avg”。

To make life easier I know for sure that Workbook_1 will only have sheets whose names are contained in Workbook_0. 为了使生活更轻松,我确定Workbook_1只会包含名称包含在Workbook_0中的工作表。 Even though it might be fair to ask why I don't sort the sheets in Workbook_1 as I create them, that's not what I need to do. 尽管问我为什么在创建工作簿_1时不对工作表中的工作表进行排序可能很公平,但这不是我需要做的。 I'm not exactly sure how to go about this without making an inefficient and hard to debug code. 我不确定如何在不使效率低下且难以调试代码的情况下进行此操作。

Any help is greatly appreciated! 任何帮助是极大的赞赏! Thanks! 谢谢!

UPDATE: @MattCrum solution works very well. 更新:@MattCrum解决方案效果很好。 Here is my final code which include a few modifications (ignores case, fixed a bug, output to a different workbook, moves additional sheets): 这是我的最终代码,其中包括一些修改(忽略大小写,修复错误,输出到其他工作簿,移动其他工作表):

Dim rngTemp As Range, rngAll as Range
Dim shtTemp As Worksheet, shtFound As Worksheet

Set rngAll = Range("A1:A3")
Set shtFound = Sheets(1)

' Sort _count sheets
For Each rngTemp In rngAll
    For Each shtTemp In Workbooks(OutputFileName).Worksheets
        If LCase(shtTemp.Name) = LCase(rngTemp.Value) & "_count" Then
            shtTemp.Move , shtFound
            Set shtFound = shtTemp
        End If
    Next
Next

' Move _avg sheets to the right of correctly ordered _count sheets
For Each rngTemp In rngAll
    For Each shtTemp In Workbooks(OutputFileName).Worksheets
        If LCase(shtTemp.Name) = LCase(rngTemp.Value) & "_time" Then
            shtTemp.Move , Sheets(LCase(rngTemp.Value) & "_count")
        End If
        If LCase(shtTemp.Name) = LCase(rngTemp.Value) & "_avg" Then
            shtTemp.Move , Sheets(LCase(rngTemp.Value) & "_time")
        End If
    Next
Next

Here's a fairly simple way to do it - not sure how fast it'll be if you have a lot of sheets but give it a go: 这是一种相当简单的方法-不确定如果您有很多工作表,但是尝试一下,它将有多快:

Sub SortSheets()

Dim rngAll As Range, rngTemp As Range
Dim shtTemp As Worksheet, shtFound As Worksheet

Set rngAll = Sheet1.Range("A1:A3")
Set shtFound = Sheets(1)

'Sort _count sheets

    For Each rngTemp In rngAll
        For Each shtTemp In ThisWorkbook.Worksheets
            If shtTemp.Name = rngTemp.Value & "_count" Then
                shtTemp.Move , shtFound
                Set shtFound = shtTemp
            End If
        Next
    Next

'Move _avg sheets to the right of correctly ordered _count sheets

    For Each rngTemp In rngAll
        For Each shtTemp In ThisWorkbook.Worksheets
            If shtTemp.Name = rngTemp.Value & "_avg" Then
                shtTemp.Move , Sheets(rngTemp & "_count")
            End If
        Next
    Next

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM