Order of hidden tabs

RodneyC

Active Member
Joined
Nov 4, 2021
Messages
278
Office Version
  1. 2016
Platform
  1. Windows
I've inherited a number of files, some have a lot of hidden tabs, in some cases as many as 20. I need to unhide these one at a time, add new data, then re-hide. When I look to find the needed hidden tab, I see all 20 but they appear to be in random order. Is there anything I can do to control the order? I'd really like to put them in alphabetical order.

Thanks in advance
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
This code will sort the hidden sheets. It requires an empty sheet named SortSht to be created. It requires the sheets to be hidden.

VBA Code:
Sub SortHiddenWS()
  Dim Sht As Worksheet
  Dim Cel As Range
  Dim R As Range
  Dim SortSht As Worksheet
  Dim X As Long
  Dim ShtCnt As Long
  Dim Astr As String
  
  Set SortSht = Sheets("SortSht")
  SortSht.Range("A:A").ClearContents
  Set R = SortSht.Range("A1")
  
  X = -1
  For Each Sht In ThisWorkbook.Worksheets
    If Sht.Visible = xlSheetHidden Then
      X = X + 1
      R.Offset(X, 0).Value = Sht.Name
    End If
  Next Sht
  
  Set R = SortSht.Range(R, R.Offset(10000, 0).End(xlUp))
  
  SortSht.Sort.SortFields.Clear
  SortSht.Sort.SortFields.Add2 Key:=R, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
  With SortSht.Sort
    .SetRange R
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
  
  ShtCnt = ThisWorkbook.Worksheets.Count
  
  For Each Cel In R
    Astr = Cel.Value
    Sheets(Astr).Move After:=Sheets(ShtCnt)
  Next Cel
  

End Sub
 
Upvote 0
This code will sort the hidden sheets. It requires an empty sheet named SortSht to be created. It requires the sheets to be hidden.

VBA Code:
Sub SortHiddenWS()
  Dim Sht As Worksheet
  Dim Cel As Range
  Dim R As Range
  Dim SortSht As Worksheet
  Dim X As Long
  Dim ShtCnt As Long
  Dim Astr As String
 
  Set SortSht = Sheets("SortSht")
  SortSht.Range("A:A").ClearContents
  Set R = SortSht.Range("A1")
 
  X = -1
  For Each Sht In ThisWorkbook.Worksheets
    If Sht.Visible = xlSheetHidden Then
      X = X + 1
      R.Offset(X, 0).Value = Sht.Name
    End If
  Next Sht
 
  Set R = SortSht.Range(R, R.Offset(10000, 0).End(xlUp))
 
  SortSht.Sort.SortFields.Clear
  SortSht.Sort.SortFields.Add2 Key:=R, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
  With SortSht.Sort
    .SetRange R
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
 
  ShtCnt = ThisWorkbook.Worksheets.Count
 
  For Each Cel In R
    Astr = Cel.Value
    Sheets(Astr).Move After:=Sheets(ShtCnt)
  Next Cel
 

End Sub
Thanks. This looks above my current skill set and this task is low priority. I'll save this and attempt in the next week. THANK YOU
 
Upvote 0
This will sort any hidden sheets* in alphabetical order and place them all after the visible sheet(s). The visible sheets will remain in the order that they currently are (but they could be sorted too if you want). No extra sheet is required.

* I have assumed that the hidden sheets are not xlVeryHidden

VBA Code:
Sub SortHiddenSheets()
  Dim AL As Object
  Dim ws As Worksheet
  Dim itm As Variant

  Set AL = CreateObject("System.Collections.ArrayList")
  For Each ws In Worksheets
    If Not ws.Visible Then
      AL.Add ws.Name
    End If
  Next ws
  If AL.Count > 0 Then
    AL.Sort
    AL.Reverse
    For Each itm In AL.ToArray
      Worksheets(itm).Move After:=Worksheets(Worksheets.Count)
    Next itm
  End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

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