我需要根据特定规则对Excel 2010中的一些工作表进行排序。在宏运行的工作簿(Workbook_0)中,我有一个单词列表,所有单词都在一行中,每列一个,例如
我的宏创建了一个新工作簿(Workbook_1),其中包含称为
的工作表我想要做的是根据Workbook_0中指定的顺序对Workbook_1中的工作表进行排序。在这个例子中,我会得到
为了让生活更轻松,我确信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 :(得分: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