Creating List of Worksheet Tab Names from Function

K

Ken Dickens

I'd like to create in my first Worksheet a table of
contents. This table would automatically list the names
of each worksheet in the workbook. I've only been able to
find SHEETNAME, which it appears is only useable in Visual
Basic. I'm hoping for a simpler solution. Is there
something in a normal Function for this? Or what is the
best method? Thx -kd
 
N

Norman Harker

Hi Ken!

Use the following formula as your base:

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)

Replace A1 by any address on a sheet you want the name of:

Example:
=MID(CELL("filename",'DU204 Prob'!A1),FIND("]",CELL("filename",'DU204
Prob'!A1))+1,255)

This only works if the file has been saved.

--
Regards
Norman Harker MVP (Excel)
Sydney, Australia
(e-mail address removed)
Excel and Word Function Lists (Classifications, Syntax and Arguments)
available free to good homes.
 
G

Gord Dibben

Ken

No built-in Function.

VBA Macro to do it.

''list of sheet names in a workbook - placed on a new worksheet
Sub ShowNames()
Set wkbkToCount = ActiveWorkbook
iRow = 1
With Sheets.Add
For Each ws In wkbkToCount.Worksheets
.Rows(iRow).Cells(1).Value = ws.Name
iRow = iRow + 1
Next
End With
End Sub

Make a backup copy of your workbook before taking further steps.

Hit ALT + F11 to enter the Visual Basic Editor.

View>Project Explorer(if not there already).

Select your workbook/project and Insert>Module. Paste the Sub ShowNames in
there.

ALT + Q to get back to Excel.

Save the workbook.

Tools>Macro>Macros. Select the ShowNames macro and "Run"

For more on getting started with macros see David McRitchie's site

http://www.mvps.org/dmcritchie/excel/getstarted.htm

Gord Dibben Excel MVP
 
K

Ken Dickens

Thx 2rrs, Gord and Norman. I appreciate the feedback - kd
-----Original Message-----
Ken

No built-in Function.

VBA Macro to do it.

''list of sheet names in a workbook - placed on a new worksheet
Sub ShowNames()
Set wkbkToCount = ActiveWorkbook
iRow = 1
With Sheets.Add
For Each ws In wkbkToCount.Worksheets
.Rows(iRow).Cells(1).Value = ws.Name
iRow = iRow + 1
Next
End With
End Sub

Make a backup copy of your workbook before taking further steps.

Hit ALT + F11 to enter the Visual Basic Editor.

View>Project Explorer(if not there already).

Select your workbook/project and Insert>Module. Paste the Sub ShowNames in
there.

ALT + Q to get back to Excel.

Save the workbook.

Tools>Macro>Macros. Select the ShowNames macro and "Run"

For more on getting started with macros see David McRitchie's site

http://www.mvps.org/dmcritchie/excel/getstarted.htm

Gord Dibben Excel MVP

wrote in message [email protected]>...
 
H

Harlan Grove

Use the following formula as your base:

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)

Replace A1 by any address on a sheet you want the name of:

Example:
=MID(CELL("filename",'DU204 Prob'!A1),FIND("]",CELL("filename",'DU204
Prob'!A1))+1,255)
...

Perhaps you missed the chicken-and-egg nature of this 'solution'.

In order for this formula to evaluate to DU204 Prob the user must enter the
string 'DU204 Prob'! *TWICE* in this formula, and he'd need to do so manually as
there's apparently no way to automate this with functions. So why wouldn't it be
easier to have him just enter the worksheet names as constant text strings into
these cells?
 
H

Harlan Grove

...
...
''list of sheet names in a workbook - placed on a new worksheet
Sub ShowNames()
Set wkbkToCount = ActiveWorkbook
iRow = 1
With Sheets.Add
For Each ws In wkbkToCount.Worksheets
.Rows(iRow).Cells(1).Value = ws.Name
iRow = iRow + 1
Next
End With
End Sub
...

Why not make it a udf? That way it could modify itself if the OP ever changed
worksheet names.


Function slst(Optional t As String = "CMS", Optional r As Range) As Variant
'----------------------------------------------------------------
'optional 1st arg specifies which sheets to include in results
'using last char of XL4 worksheet extensions: xlC - charts,
'xlM - macros, xlS - [work]sheets -- all other chars ignored
'optional 2nd arg used to specify which *OPEN* workbook's sheets
'1st defaults to all sheets, latter defaults to workbook which
'contains the calling formula.
'----------------------------------------------------------------
Const C As Long = 1, M As Long = 2, S As Long = 3

Dim rv As Variant, tt(1 To 3) As Boolean, x As Variant, n As Long

If r Is Nothing Then

If TypeOf Application.Caller Is Range Then
Set r = Application.Caller

Else
Set r = ActiveCell

End If

End If

If InStr(1, t, "C", vbTextCompare) > 0 Then tt(C) = True
If InStr(1, t, "M", vbTextCompare) > 0 Then tt(M) = True
If InStr(1, t, "S", vbTextCompare) > 0 Then tt(S) = True

ReDim rv(1 To r.Parent.Parent.Sheets.Count)

For Each x In Application.Caller.Parent.Parent.Sheets

If (x.Type = -4169 And tt(C)) Or ((x.Type = xlExcel4MacroSheet _
Or x.Type = xlExcel4IntlMacroSheet) And tt(M)) _
Or (x.Type = xlWorksheet And tt(S)) Then
n = n + 1
rv(n) = x.Name
End If

Next x

ReDim Preserve rv(1 To n)

slst = Application.WorksheetFunction.Transpose(rv)

End Function


Best not to make this volatile.
 
K

Ken

Harlan,
You're saying that it could recognize automatically
when you change a Sheetname and it would display the new
sheetname in the list? Also, is UDF, User Defined
Function? Thx - kd
-----Original Message-----
...
...
''list of sheet names in a workbook - placed on a new worksheet
Sub ShowNames()
Set wkbkToCount = ActiveWorkbook
iRow = 1
With Sheets.Add
For Each ws In wkbkToCount.Worksheets
.Rows(iRow).Cells(1).Value = ws.Name
iRow = iRow + 1
Next
End With
End Sub
...

Why not make it a udf? That way it could modify itself if the OP ever changed
worksheet names.


Function slst(Optional t As String = "CMS", Optional r As Range) As Variant
'--------------------------------------------------------- -------
'optional 1st arg specifies which sheets to include in results
'using last char of XL4 worksheet extensions: xlC - charts,
'xlM - macros, xlS - [work]sheets -- all other chars ignored
'optional 2nd arg used to specify which *OPEN* workbook's sheets
'1st defaults to all sheets, latter defaults to workbook which
'contains the calling formula.
'--------------------------------------------------------- -------
Const C As Long = 1, M As Long = 2, S As Long = 3

Dim rv As Variant, tt(1 To 3) As Boolean, x As Variant, n As Long

If r Is Nothing Then

If TypeOf Application.Caller Is Range Then
Set r = Application.Caller

Else
Set r = ActiveCell

End If

End If

If InStr(1, t, "C", vbTextCompare) > 0 Then tt(C) = True
If InStr(1, t, "M", vbTextCompare) > 0 Then tt(M) = True
If InStr(1, t, "S", vbTextCompare) > 0 Then tt(S) = True

ReDim rv(1 To r.Parent.Parent.Sheets.Count)

For Each x In Application.Caller.Parent.Parent.Sheets

If (x.Type = -4169 And tt(C)) Or ((x.Type = xlExcel4MacroSheet _
Or x.Type = xlExcel4IntlMacroSheet) And tt(M)) _
Or (x.Type = xlWorksheet And tt(S)) Then
n = n + 1
rv(n) = x.Name
End If

Next x

ReDim Preserve rv(1 To n)

slst = Application.WorksheetFunction.Transpose(rv)

End Function


Best not to make this volatile.
 
K

Ken

Harlan,
What is DU204 Prob ? Thx - kd
-----Original Message-----
Use the following formula as your base:

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1)) +1,255)

Replace A1 by any address on a sheet you want the name of:

Example:
=MID(CELL("filename",'DU204 Prob'!A1),FIND("]",CELL ("filename",'DU204
Prob'!A1))+1,255)
...

Perhaps you missed the chicken-and-egg nature of this 'solution'.

In order for this formula to evaluate to DU204 Prob the user must enter the
string 'DU204 Prob'! *TWICE* in this formula, and he'd need to do so manually as
there's apparently no way to automate this with
functions. So why wouldn't it be
 
H

Harlan Grove

You're saying that it could recognize automatically
when you change a Sheetname and it would display the new
sheetname in the list? Also, is UDF, User Defined
Function? Thx - kd
...

First, yes, UDF means user-defined function.

This is the sort of UDF that doesn't make sense to be volatile because its
result will change infrequently. When it's not volatile, it won't automatically
and immediately show any changes in worksheet names or worksheet order, but a
full recalc - [Alt]+[Ctrl]+[F9] - will update it.

There are tricks for making nonvolatile functions effectively volatile. In this
case, since slst() returns an array of text strings, you could do this with

=slst()&LEFT(NOW(),0)

The NOW function is volatile, so it'll force Excel to re-evaluate slst() upon
each recalc, and changing worksheet names or ordering does trigger (normal)
recalc.
 
H

Harlan Grove

What is DU204 Prob ? Thx - kd

Norman's sample worksheet name. The problem here is that getting the name of a
different worksheet using the MID(CELL...) idiom requires entering that
worksheet's name twice in the formula so that the formula could return it. This
is kinda like driving to a store 1 mile west of your home by driving past the
store and parking 1 mile west of it, walking to it from there, then walking back
to your car after you've finished shopping, and driving the 2 miles east back
home.
 
N

Norman Harker

Hi Ken!

Probably the easiest solution is to use the base formula in a constant
cell on each sheet and refer to that cell in your summary sheet.

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)

At least it will update if sheet name is changed.


--
Regards
Norman Harker MVP (Excel)
Sydney, Australia
(e-mail address removed)
Excel and Word Function Lists (Classifications, Syntax and Arguments)
available free to good homes.
Ken said:
Harlan,
What is DU204 Prob ? Thx - kd
-----Original Message-----
Use the following formula as your base:

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1)) +1,255)

Replace A1 by any address on a sheet you want the name of:

Example:
=MID(CELL("filename",'DU204 Prob'!A1),FIND("]",CELL ("filename",'DU204
Prob'!A1))+1,255)
...

Perhaps you missed the chicken-and-egg nature of this 'solution'.

In order for this formula to evaluate to DU204 Prob the user must enter the
string 'DU204 Prob'! *TWICE* in this formula, and he'd need to do so manually as
there's apparently no way to automate this with
functions. So why wouldn't it be
easier to have him just enter the worksheet names as constant text strings into
these cells?
 
N

Norman Harker

Hi!

Or a fairly simple subroutine placed in a module:

Sub ListWorkSheets()
'Dumps a list of worksheet names in active cell and under
Dim N As Integer
For N = 1 To ActiveWorkbook.Worksheets.Count
ActiveCell(N, 1) = Worksheets(N).Name
Next
End Sub

But you've got to remember to re-run it if you change the sheet names.
--
Regards
Norman Harker MVP (Excel)
Sydney, Australia
(e-mail address removed)
Excel and Word Function Lists (Classifications, Syntax and Arguments)
available free to good homes.
 
H

Harlan Grove

Or a fairly simple subroutine placed in a module: ...
But you've got to remember to re-run it if you change the sheet names.

As has already been discussed in another branch of this thread.
 
K

Ken

Thanks Norman, Harlan and Flamikey, you've been very
helpful. I went to the Web site and pulled in the ASAP
Utilities, this is a great set of tools. Thx again. kd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top