Ordinare fogli di lavoro in una cartella Excel
lunedì, 17 settembre 2007
E' possibile ordinare in maniera automatica i fogli di lavoro presenti in una cartella di Excel, secondo un ordine alfabetico o numerico.
Vediamo come:
Ordinamento alfabetico
Aprire il file che contiene i fogli che devono essere ordinati.
A questo punto dovendo lavorare tramite codice VBA apriamo la relativa finestra:
In Excel XP o 2003:
Menù Strumenti - Macro - Visual Basic Editor
In Excel 2007:
Bottone di Office - Pulsante Opzioni di Excel - Impostazioni generali - Opzione "Mostra scheda Sviluppo sulla barra multifunzione"
A questo punto ritornando nella finestra di Excel verrà visualizzata anche la Scheda Sviluppo nella quale troviamo il pulsante Visual Basic
Nella finestra Visual Basic inserire un nuovo modulo (menù Inserisci - Modulo) e copiare il codice riportato sotto:
Sub SortWorksheets()
Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
SortDescending = False
If ActiveWindow.SelectedSheets.Count = 1 Then
FirstWSToSort = 1
LastWSToSort = Worksheets.Count
Else
With ActiveWindow.SelectedSheets
For N = 2 To .Count
If .Item(N - 1).Index <> .Item(N).Index - 1 Then
MsgBox "You cannot sort non-adjacent sheets"
Exit Sub
End If
Next N
FirstWSToSort = .Item(1).Index
LastWSToSort = .Item(.Count).Index
End With
End If
For M = FirstWSToSort To LastWSToSort
For N = M To LastWSToSort
If SortDescending = True Then
If UCase(Worksheets(N).Name) > UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
End If
Next N
Next M
End Sub
Chiudere quindi la finestra di Visual Basic e tornare in Excel.
Passiamo quindi all'elenco delle Macro
In Excel 2003 Strumenti -> Macro -> Macro
In Excel 2007 Scheda Sviluppo -> Pulsante Macro
Eseguire quindi la Macro "SortWorksheets"
I fogli presenti nella cartella di Excel saranno ora elencati secondo l'ordinamento alfabetico.
L'ordinamento verrà effettuato in senso crescente se invece è necessario ordinare in senso decrescete allora si dovrà impostare a True il valore SortDescending.
Se vogliamo rendere disponibile questa funzione in modo tale da trovarla all'avvio di ogni cartella di Excel possiamo creare un componente aggiuntivo come specificato qui.
Ordinamento numerico
Aprire il file che contiene i fogli che devono essere ordinati.
A questo punto dovendo lavorare tramite codice VBA apriamo la relativa finestra:
In Excel XP o 2003:
Menù Strumenti - Macro - Visual Basic Editor
In Excel 2007:
Bottone di Office - Pulsante Opzioni di Excel - Impostazioni generali - Opzione "Mostra scheda Sviluppo sulla barra multifunzione"
A questo punto ritornando nella finestra di Excel verrà visualizzata anche la Scheda Sviluppo nella quale troviamo il pulsante Visual Basic
Nella finestra Visual Basic inserire un nuovo modulo (menù Inserisci - Modulo) e copiare il codice riportato sotto:
Sub SortWorksheetsNumeric()
Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
SortDescending = False
If ActiveWindow.SelectedSheets.Count = 1 Then
FirstWSToSort = 1
LastWSToSort = Worksheets.Count
Else
With ActiveWindow.SelectedSheets
For N = 2 To .Count
If .Item(N - 1).Index <> .Item(N).Index - 1 Then
MsgBox "You cannot sort non-adjacent sheets"
Exit Sub
End If
Next N
FirstWSToSort = .Item(1).Index
LastWSToSort = .Item(.Count).Index
End With
End If
For M = FirstWSToSort To LastWSToSort
For N = M To LastWSToSort
If SortDescending = True Then
If CInt(Mid(Worksheets(N).Name, 6)) > _
CInt(Mid(Worksheets(M).Name, 6)) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
Else
If CInt(Mid(Worksheets(N).Name, 6)) < _
CInt(Mid(Worksheets(M).Name, 6)) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
End If
Next N
Next M
End Sub
Chiudere quindi la finestra di Visual Basic e tornare in Excel.
Passiamo quindi all'elenco delle Macro
In Excel 2003 Strumenti -> Macro -> Macro
In Excel 2007 Scheda Sviluppo -> Pulsante Macro
Eseguire quindi la Macro "SortWorksheetsNumeric"
I fogli presenti nella cartella di Excel saranno ora elencati secondo l'ordinamento numerico.
L'ordinamento verrà effettuato in senso crescente se invece è necessario ordinare in senso decrescete allora si dovrà impostare a True il valore SortDescending.
Se vogliamo rendere disponibile questa funzione in modo tale da trovarla all'avvio di ogni cartella di Excel possiamo creare un componente aggiuntivo come specificato qui.
Tags:
posted by Andrea Perotti @ 17.06 Permalink ,
7 Comments:
- At 21 dicembre 2007 15.36, Pam said...
-
Mitico!!!!
Grazie!!!
Pamela - At 21 dicembre 2007 16.08, Andrea Perotti said...
-
Prego! :-)
- At 8 maggio 2008 19.17, said...
-
suggerisco un'alternativa con codice ridotto:
Public Sub Ordina_fogli()
Dim a As Integer
Dim b As Integer
For a = 1 To Sheets.Count
For b = 1 To Sheets.Count - 1
If UCase(Sheets(b).Name) > UCase(Sheets(a).Name) Then
Sheets(b).Move after:=Sheets(b + 1)
End If
Next
Next
End Sub - At 9 maggio 2008 08.54, Pam said...
-
Ri grazie ancora!
- At 3 giugno 2008 12.06, Aless{a}ndro said...
-
Grazie, molto utile!
- At 19 novembre 2008 12.35, said...
-
Grazie, ho usato il tuo metodo ed è stata quasi magia!!
- At 3 settembre 2009 16.52, said...
-
Grazie mille!!
Veramente utile e ben fatto.





