Kategorie szkoleń | Egzaminy | Kontakt

Odpowiedzi (2)

  • 12

Jedynym sposobem automatycznym, jest makro napisane w VBA (nie da się go zarejestrować), np.:

Sub ZamienMiejscami(x As Integer, y As Integer)
        Dim a As Worksheet, b As Worksheet
        Set a = Sheets(x)
        Set b = Sheets(y)
        a.Move Before:=b
        b.Move Before:=Sheets(x)
End Sub

Function QuickSortPodziel(L As Integer, R As Integer, 
                                v As Integer) As Integer
        Dim pV As String
        Dim I as Integer, x As Integer, y As Integer
        pV = Sheets(v).Name
        ZamienArkusze v, R
        x = L
        For I = L To R - 1
                If Sheets(I).Name <= pv Then
                        ZamienArkusze y, x
                        x = x + 1
                End If
        Next
        ZamienArkusze x, R
        QuickSortPodziel = x
End Function

Sub QuickSortArkusze(L As Integer, R As Integer)
        Dim pV As Integer
        Dim nPV As Integer
        If R > L Then
                nPV = QuickSortPodziel(L, R, L)
                QuickSortArkusze L, nPV - 1
                QuickSortArkusze nPV + 1, R
        End If
End Sub

Public Sub SortujArkusze()
        QuickSortArkusze 1, Sheets.Count
End Sub

Wywołanie makra "Sortuj Arkusze" posortuje arkusze w kolejności alfabetycznej rosnącej.

  • Odpowiedział
  • @ | 26.05.2014
  • TRENER ALTKOM AKADEMII
Komentarze
Piotrek. A czy można zrobić, żeby to makro było w oddzielnym pliku i następnie otwieram plik, w którym chcę posortować arkusze i otwieram plik, w którym jest zapisane makro i następnie wybieram np. przycisk sortuj w tym arkuszu?
Skomentował : @ TRENER ALTKOM AKADEMII ,28.05.2014
  • 13

 

Sub SortowanieArkuszy_AZ()

Dim i As Integer
Dim j 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 i = 2 To .Count
            If .Item(i - 1).Index <> .Item(i).Index - 1 Then
                MsgBox "You cannot sort non-adjacent sheets"
                Exit Sub
            End If
        Next i
        FirstWSToSort = .Item(1).Index
        LastWSToSort = .Item(.Count).Index
     End With
End If

For j = FirstWSToSort To LastWSToSort
    For i = j To LastWSToSort
        If SortDescending = True Then
            If UCase(Worksheets(i).Name) > UCase(Worksheets(j).Name) Then
                Worksheets(i).Move Before:=Worksheets(j)
            End If
        Else
            If UCase(Worksheets(i).Name) < UCase(Worksheets(j).Name) Then
               Worksheets(i).Move Before:=Worksheets(j)
            End If
        End If
     Next i
Next j

End Sub

Kiedyś używałem powyższego makra (znalezione w sieci). Sortuje alfabetycznie arkusze w podobny sposób jak sortowanie komórek w Excelu.

 

Przemo
  • Odpowiedział
  • @ Przemo | 27.05.2014
    • laureat
    • 59
    • 24
    • 71