sorting many worksheets

daveturner80

New Member
Joined
May 11, 2011
Messages
18
I have a workbook with 7 sheets in it. I am a member of a fire dept. and I use Col. A as our members names, last name,first name. When one of our female members gets married and changes her last name to her husband's last name,
I need to change her name and sort all the sheets. I am using excel 2010 and would like to know if this is possible to do? I need it to be able to sort all the columns in all the sheets.
Thanks,
Dave
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Code:
Sub SortSheets()

' This routine sorts the sheets of the
' active workbook in ascending order.

    Dim SheetNames() As String
    Dim SheetHidden() As Boolean
    Dim i As Integer
    Dim SheetCount As Integer
    Dim VisibleWins As Integer
    Dim Item As Object
    Dim OldActive 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
    
    ' Disable Ctrl+Break
    Application.EnableCancelKey = xlDisabled
    
    ' Get the number of sheets
    SheetCount = ActiveWorkbook.Sheets.Count
    
    ' Redimension the arrays
    ReDim SheetNames(1 To SheetCount)
    ReDim SheetHidden(1 To SheetCount)
    
    ' Store a reference to the active sheet
    Set OldActive = ActiveSheet
    
    ' Fill array with sheet names
    For i = 1 To SheetCount
        SheetNames(i) = ActiveWorkbook.Sheets(i).Name
    Next i
    
    ' Fill array with hidden status of sheets
    For i = 1 To SheetCount
        SheetHidden(i) = Not ActiveWorkbook.Sheets(i).Visible
        ' Unhide hidden sheets
        If SheetHidden(i) Then ActiveWorkbook.Sheets(i).Visible = True
    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
    
    ' Re-hide sheets
    For i = 1 To SheetCount
        If SheetHidden(i) Then ActiveWorkbook.Sheets(i).Visible = False
    Next i
    
    ' Reactivate the original active sheet
    OldActive.Activate
    
End Sub

Private Sub BubbleSort(List() As String)
    ' Sorts the List array in ascending order
    Dim First As Integer, Last As Integer
    Dim i As Integer, j As Integer
    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
 
Upvote 0
If it "didn't do anything", then your sheets seem to be already sorted!
Try to place sheets in random order and run macro again.
 
Upvote 0
I am not getting your concept of putting the sheets in random order?
What is that going to do with sorting the names in all the sheets that I have in the workbook?
I went into all the sheets and changed my name from Turner,Dave to Miller,Dave and ran the macro and it didn't sort anything.
What am I doing or not doing to get these sheets sorted?
Thanks for all your time.
Dave
 
Upvote 0
I finally looked and saw that your macro is working correctly, however that isn't what I need to do.
I have a workbook with 7 sheets in it. I am a member of a fire dept. and I use Col. A in all the sheets as our members names, last name,first name. When one of our female members gets married and changes her last name to her husband's last name,
I need to change her name in all the sheets and then sort all the sheets. I am using excel 2010 and would like to know if this is possible to do? I need it to be able to sort all the columns in all the sheets.
I hope this makes sense to you. I don't want to sort the sheets I need to sort the names in each sheet.
thanks,
Dave
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,550
Members
452,927
Latest member
rows and columns

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top