2013-01-31 21 views
0

我需要根据特定规则对Excel 2010中的某些工作表进行排序。在工作簿,其中宏运行(Workbook_0)我有词语的列表,所有在一排,每列一个,例如根据一定范围内的数据组织/排列工作簿表格

  • 一个:“芒果” B:“苹果” C:“香蕉”。我将此范围保存在 类型范围的变量中。

我的宏创建一个新的工作簿(Workbook_1)与片材称为,例如,

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

我想要做的是根据Workbook_0中指定的顺序对Workbook_1中的工作表进行排序。在这个例子中,我将获得

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

为了让生活更轻松我肯定知道Workbook_1只有名称包含在Workbook_0中的工作表。尽管问我为什么不在Workbook_1中对工作表进行排序是因为我创建了工作簿,但这不是我需要做的。我并不确定如何在没有低效率和难以调试的代码的情况下去解决这个问题。

任何帮助,非常感谢!谢谢!

更新:@MattCrum解决方案工作得很好。这是我最后的代码,其中包括了一些修改(忽略的情况下,修正了一个错误,输出到不同的工作簿,移动附加纸张):

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 

回答

0

这是一个相当简单的方法来做到这一点 - 不知道如何快速将是如果你有很多表,但给它一个去:

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 
+0

谢谢,它看起来应该工作,我要去尝试它。如果我要使搜索不区分大小写,那么我可以使用'Lcase()'函数,对吧? – squalho

+0

感谢@MattCrum,你钉了它,甚至比你想象的还要好!我必须分类的图纸只是整个图纸的一个子集。您的代码不会干扰需要排序的表单之前或之后的表单。我只做了一些我在上面报告的小修改。特别是在第二次排序时,我用'Sheets(rngTemp.value&“_count”)替换了'Sheets(rngTemp&“_count”)' – squalho

相关问题