Hvis det må være en VBA løsning, kan denne måske bruges?
Sub SortSheets() ' This routine sorts the sheets of the ' active workbook in ascending order. ' Use Ctrl+Shift+S to execute Dim SheetNames() As String Dim i As Long Dim SheetCount As Long Dim OldActiveSheet As Object If ActiveWorkbook Is Nothing Then Exit Sub ' No active workbook SheetCount = ActiveWorkbook.Sheets.Count ' Check for protected workbook structure If ActiveWorkbook.ProtectStructure Then MsgBox ActiveWorkbook.Name & " is protected.", _ vbCritical, "Cannot Sort Sheets." Exit Sub End If ' Make user verify If MsgBox("Sort the sheets in the active workbook?", _ vbQuestion + vbYesNo) <> vbYes Then Exit Sub ' Disable Ctrl+Break Application.EnableCancelKey = xlDisabled ' Get the number of sheets SheetCount = ActiveWorkbook.Sheets.Count ' Redimension the array ReDim SheetNames(1 To SheetCount) ' Store a reference to the active sheet
Set OldActiveSheet = ActiveSheet ' Fill array with sheet names For i = 1 To SheetCount SheetNames(i) = ActiveWorkbook.Sheets(i).Name Next i ' Sort the array in ascending order Call BubbleSort(SheetNames) ' Turn off screen updating Application.ScreenUpdating = False ' Move the sheets For i = 1 To SheetCount ActiveWorkbook.Sheets(SheetNames(i)).Move _ Before:=ActiveWorkbook.Sheets(i) Next i ' Reactivate the original active sheet OldActiveSheet.Activate End Sub
Sub BubbleSort(List() As String) ' Sorts the List array in ascending order Dim First As Long, Last As Long Dim i As Long, j As Long Dim Temp As String First = LBound(List) Last = UBound(List)
For i = First To Last - 1 For j = i + 1 To Last If List(i) > List(j) Then Temp = List(j) List(j) = List(i) List(i) = Temp End If Next j Next i End Sub
Ups, jeg kom til at se at du ville have sorteret fra højre mod venstre:
Sub SortSheets() ' This routine sorts the sheets of the ' active workbook in ascending order. ' Use Ctrl+Shift+S to execute Dim SheetNames() As String Dim i As Long Dim SheetCount As Long Dim OldActiveSheet As Object If ActiveWorkbook Is Nothing Then Exit Sub ' No active workbook SheetCount = ActiveWorkbook.Sheets.Count ' Check for protected workbook structure If ActiveWorkbook.ProtectStructure Then MsgBox ActiveWorkbook.Name & " is protected.", _ vbCritical, "Cannot Sort Sheets." Exit Sub End If ' Make user verify If MsgBox("Sort the sheets in the active workbook?", _ vbQuestion + vbYesNo) <> vbYes Then Exit Sub ' Disable Ctrl+Break Application.EnableCancelKey = xlDisabled ' Get the number of sheets SheetCount = ActiveWorkbook.Sheets.Count ' Redimension the array ReDim SheetNames(1 To SheetCount) ' Store a reference to the active sheet
Set OldActiveSheet = ActiveSheet ' Fill array with sheet names For i = 1 To SheetCount SheetNames(i) = ActiveWorkbook.Sheets(i).Name Next i ' Sort the array in ascending order Call BubbleSort(SheetNames) ' Turn off screen updating Application.ScreenUpdating = False ' Move the sheets For i = 1 To SheetCount ActiveWorkbook.Sheets(SheetNames(i)).Move _ Before:=ActiveWorkbook.Sheets(i) Next i ' Reactivate the original active sheet OldActiveSheet.Activate End Sub
Sub BubbleSort(List() As String) ' Sorts the List array in ascending order Dim First As Long, Last As Long Dim i As Long, j As Long Dim Temp As String First = LBound(List) Last = UBound(List)
For i = First To Last - 1 For j = i + 1 To Last If List(i) < List(j) Then Temp = List(j) List(j) = List(i) List(i) = Temp End If Next j Next i End Sub
Du går ind i VBA- alt+F11, opretter et ny modul via Insert-New module.
Herefter kopierer du hele koden ind i modulet. Herefter kan du køre den ligesom du ville køre en alm. makro. Hvis du skal have uddybet det mere, siger du bare til. Hvis der skal sorteres fra venstre til højre skal du bruge koden i mit første svar.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.