This is a custom sheet function.
It works both in a sheet or in a sub.
Sheet syntax:
=shName()
which returns the name of the active sheet.
Function shName(Optional CellRef As Range) As String
'Get active sheet name function!
Application.Volatile
On Error GoTo myErr
If CellRef Is Nothing Then
'User did not include a cell Refrence!
shName = Application.Caller.Parent.Name
Else
'Use the cell reference added to the function by the user!
shName = CellRef.Parent.Name
End If
GoTo myEnd
myErr:
MsgBox "You may use this Function, on a" & vbCr & _
"WorkSheet with or without a" & vbCr & _
"Cell Reference Parameter." & vbCr & vbCr & _
"Like: =shName(A1) or =shName()" & vbCr & vbCr & _
"To use this Function in a Sub as code," & vbCr & _
"you must add a Cell Reference as a" & vbCr & _
"Parameter!" & vbCr & vbCr & _
"Like: shName([A1])" & vbCr & _
"Which returns the currently active" & vbCr & _
"Sheet Name."
myEnd:
End Function