2014-01-15 82 views
-1

如果我在我的excel文檔中有6個表格命名並按此順序排列:O1,O2,O3,O1_#2,O2#2,O3#3基於表格名稱通過宏重新排序Excel表格

是否可以編寫一個宏來改變這些表單的順序? 這就是我要尋找的順序: O1,O1#2,O2,O2#2,O3,O3#2

感謝

+1

我建議你檢查一下你的例子,並試着用它來嘗試自己創建這樣一個宏。你遇到了什麼問題? – pnuts

回答

1

如果我理解正確的話,你需要按字母頁面排序order:

Sub SortSheets() 

    Dim shNames As Collection 
    Dim i As Long, j As Long 
    Dim temp As String 
    Dim sh As Worksheet 

    Set shNames = New Collection 

    'add sheet names in collection 
    For Each sh In ThisWorkbook.Worksheets 
     shNames.Add sh.Name, sh.Name 
    Next sh 

    'bubble sort 
    For i = 1 To shNames.Count - 1 
     For j = i + 1 To shNames.Count 
      If shNames(i) > shNames(j) Then 
       temp = shNames(j) 
       shNames.Remove j 
       shNames.Add temp, temp, i 
      End If 
     Next j 
    Next i 

    ' move sheets 
    For i = shNames.Count - 1 To 1 Step -1 
     Worksheets(shNames(i)).Move Before:=Sheets(1) 
    Next i 

End Sub