Pois é, estamos na versão 2013 da maior suíte de trabalho voltada para escritórios e ela ainda não contempla um mecanismo ou comando para ordenar as planilhas (sheets) dentro de um arquivo (workbook).
Com esta função, você pode classificar algumas ou todas as planilhas pelo nome, em qualquer ordem crescente ou decrescente.
Este código consiste em uma função que usa parâmetros para controlar o comportamento. Você precisa criar alguns procedimentos que levam as planilha à classificação.
ORDENANDO POR NOME
Public Function SortWorksheetsByName(ByVal FirstToSort As Long, _ByVal LastToSort As Long, _ByRef ErrorText As String, _Optional ByVal SortDescending As Boolean = False, _Optional ByVal Numeric As Boolean = False) As Boolean''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' SortWorksheetsByName' This sorts the worskheets from FirstToSort to LastToSort by name' in either ascending (default) or descending order. If successful,' ErrorText is vbNullString and the function returns True. If' unsuccessful, ErrorText gets the reason why the function failed' and the function returns False. If you include the Numeric' parameter and it is True, (1) all sheet names to be sorted' must be numeric, and (2) the sort compares names as numbers, not' text.'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Dim M As LongDim N As LongDim WB As WorkbookDim B As BooleanSet WB = Worksheets.ParentErrorText = vbNullStringIf WB.ProtectStructure = True ThenErrorText = "Workbook is protected."SortWorksheetsByName = FalseEnd If'''''''''''''''''''''''''''''''''''''''''''''''' If First and Last are both 0, sort all sheets.''''''''''''''''''''''''''''''''''''''''''''''If (FirstToSort = 0) And (LastToSort = 0) ThenFirstToSort = 1LastToSort = WB.Worksheets.CountElse'''''''''''''''''''''''''''''''''''''''' More than one sheet selected. We' can sort only if the selected' sheet are adjacent.'''''''''''''''''''''''''''''''''''''''B = TestFirstLastSort(FirstToSort, LastToSort, ErrorText)If B = False ThenSortWorksheetsByName = FalseExit FunctionEnd IfEnd IfIf Numeric = True ThenFor N = FirstToSort To LastToSortIf IsNumeric(WB.Worksheets(N).Name) = False Then' can't sort non-numeric namesErrorText = "Not all sheets to sort have numeric names."SortWorksheetsByName = FalseExit FunctionEnd IfNext NEnd If'''''''''''''''''''''''''''''''''''''''''''''' Do the sort, essentially a Bubble Sort.'''''''''''''''''''''''''''''''''''''''''''''For M = FirstToSort To LastToSortFor N = M To LastToSortIf SortDescending = True ThenIf Numeric = False ThenIf StrComp(WB.Worksheets(N).Name, WB.Worksheets(M).Name, vbTextCompare) > 0 ThenWB.Worksheets(N).Move before:=WB.Worksheets(M)End IfElseIf CLng(WB.Worksheets(N).Name) > CLng(WB.Worksheets(M).Name) ThenWB.Worksheets(N).Move before:=WB.Worksheets(M)End IfEnd IfElseIf Numeric = False ThenIf StrComp(WB.Worksheets(N).Name, WB.Worksheets(M).Name, vbTextCompare) < 0 ThenWB.Worksheets(N).Move before:=WB.Worksheets(M)End IfElseIf CLng(WB.Worksheets(N).Name) < CLng(WB.Worksheets(M).Name) ThenWB.Worksheets(N).Move before:=WB.Worksheets(M)End IfEnd IfEnd IfNext NNext MSortWorksheetsByName = TrueEnd Function
Nenhum comentário:
Postar um comentário