Skip to content

Instantly share code, notes, and snippets.

@botaor
Created March 13, 2013 10:52
Show Gist options
  • Save botaor/5151052 to your computer and use it in GitHub Desktop.
Save botaor/5151052 to your computer and use it in GitHub Desktop.
Get the names of the sheets in an Excel Workbook
Function SHEETNAME(Optional ShtOrder As Long = 0, _
Optional ByVal IncludeHiddenSheet As Boolean = False) As Variant
'// Developed by Kris @ ExcelFox.com
Dim ShtCount As Long
Dim i As Long
Dim n As Long
ShtCount = ThisWorkbook.Worksheets.Count
If ShtOrder = 0 Then
SHEETNAME = ActiveSheet.Name
Exit Function
End If
SHEETNAME = CVErr(xlErrRef)
Application.Volatile
SHEETNAME = ""
With ThisWorkbook
If IncludeHiddenSheet Then
If ShtOrder > ShtCount Then Exit Function
SHEETNAME = .Worksheets(ShtOrder).Name
Else
For i = 1 To ShtCount
If .Worksheets(i).Visible = -1 Then
n = n + 1
If n = ShtOrder Then
SHEETNAME = .Worksheets(i).Name
Exit Function
End If
End If
Next
End If
End With
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment