Hi David!
Basic construction technique was to use the John Walkenbach termed
megaformula approach.
The answer is built up in a series of cells. When you have the answer
in a cell you then substitute references to earlier cells in the
process by the formulas that are contained in those cells. It looks
more complex than it is and as a general rule I would discourage the
approach and favour building the formula in a series of cells that you
might then hide. When you come back to megaformulas, they can be
extremely difficult to understand and amend and even more difficult
for someone else to understand and amend. In any event, a UDF would
generally be preferred.
The approach noted that with a single sheet workbook with the sheet
name the same as the workbook name the standard:
A4:
=CELL("filename",A1)
returned (eg)
C:\My Files\NewsGroups\Delete2 OK.xls
This now has to be parsed to strip out the file name.
A6:
=LEN(A4)-LEN(SUBSTITUTE(A4,"\",""))
Returns the fact that there are three \ in the file path
A7:
=SUBSTITUTE(A4,"\","*",A6)
Returns:
C:\My Files\NewsGroups*Delete2 OK.xls
I can now parse out the file name:
A9:
=MID(A7,FIND("*",A7)+1,32)
Returns:
Delete2 OK.xls
I can now substitute the formula in A7 for the references to A7 and
this gives me:
=MID(SUBSTITUTE(A4,"\","*",A6),FIND("*",SUBSTITUTE(A4,"\","*",A6))+1,32)
I now substitute the formulas in A4 and A6 for the references to those
cells and that gives me:
=MID(SUBSTITUTE(CELL("filename",A1),"\","*",LEN(A4)-LEN(SUBSTITUTE(A4,"\",""))),FIND("*",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(A4)-LEN(SUBSTITUTE(A4,"\",""))))+1,32)
The substitutions of A6 still leave references to A4 which need
substituting by the formula in A4 and that gives me:
=MID(SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))),FIND("*",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))+1,32)
Now this works OK until I add a sheet to the file. In that (normal)
case I get a return of:
[Delete2 OK.xls]Delete2 OK
For the normal cases I can use the standard formula:
=MID(CELL("filename",A1),FIND("[",CELL("filename",A1),1)+1,FIND("]",CELL("filename",A1),1)-FIND("[",CELL("filename",A1),1)-1)
Returns: Delete2 OK.xls
So I have a normal case and an abnormal case formula. I use an IF
function to select between them and I choose to test for abnormality
by seeing if the return of the CELL formula contains a [. If the
length of the return without [ is the same as the length with any [,
then the abnormal case applies. Otherwise it is normal case.
That gives me the final version for getting the filename:
=IF(LEN(CELL("filename",A1))=LEN(SUBSTITUTE(CELL("filename",A1),"[","")),MID(SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))),FIND("*",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))+1,32),MID(CELL("filename",A1),FIND("[",CELL("filename",A1),1)+1,FIND("]",CELL("filename",A1),1)-FIND("[",CELL("filename",A1),1)-1))
Returns:
Delete2 OK.xls
Checks OK for both normal cases with more than one sheet or where the
name of the sheet is different from the name of the workbook. Checks
OK for the special case where there's only one sheet and it has the
same name as the workbook.
I could use an IF ISERROR approach to make the selection but rejected
that because there could be other sources of error such as a non-saved
file.
The sheet formula uses the same construction approach. I could just
strip off the extension from the file name in abnormal cases (note the
names are the same). However, I can do this more directly because when
I have:
C:\My Files\NewsGroups*Delete2 OK.xls
The sheet name the same as the file name (without the extension xls)
and I can extract that using MID and a calculation of the number of
characters needed from after * and before the .
That gave me the "abnormal" formula:
=MID(SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))),FIND("*",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))+1,FIND(".",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))-FIND("*",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))-1)
The "normal" formula is:
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,32)
So using the same IF function selection criteria as before:
That gives me a method of getting the sheet name in both normal and
abnormal cases:
=IF(LEN(CELL("filename",A1))=LEN(SUBSTITUTE(CELL("filename",A1),"[","")),MID(SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))),FIND("*",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))+1,FIND(".",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))-FIND("*",SUBSTITUTE(CELL("filename",A1),"\","*",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))-1),MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,32))
Like I said, I'm sure that these could be made more efficient. I
wouldn't recommend the approach over building it up in steps in
separate cells.
And in any event it would be much better to use the User Defined
Function:
Function SHEETNAME() As String
'Volatile covers re-naming but can impact on performance.
Application.Volatile True
SHEETNAME = [Cell("FileName",A1]
SHEETNAME = Application.ActiveSheet.Name
End Function
This UDF does not suffer from the problem that Jack faced when he hit
a file with only one sheet with the same name as the file.
--
Regards
Norman Harker MVP (Excel)
Sydney, Australia
(e-mail address removed)
David McRitchie said:
Hi Norman,
Okay, I see that, now. How in the world did you come
up with that LONG formula, what is it's design, and how
did you manage that without making mistakes. Did
you construct it on a worksheet.
Are you guys running on Macs, or is it that XP allows you
to create worksheets with characters previously prohibited
for worksheet names such as a backward slash, or a colon.
In Excel 2000 this is what I would get, if I tried to rename
worksheet:
-------------------------
NAME = While renaming a sheet or chart, you entered an
invalid name. Try one of the following:
• Make sure the name you entered does not exceed 31 characters.
• Make sure the name does not contain any of the
following characters: : \ / ? * [ or ]
• Make sure you did not leave the name blank
--------------------------
--David McRitchie, Microsoft MVP - Excel
Norman Harker said:
Hi David!
Look a little closer. We have a special case here where the usual
parsing of CELL doesn't work because of an unusual case of what it
returns where file name and sheet name are the same and there is
only
one sheet in the file.