Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Not Re-evaluating Formula

Status
Not open for further replies.

ceecld

Technical User
Jul 17, 2003
68
US
Hi Guys,

Ive got the following code which takes the name of the worksheet modifies it slighty and displays it in a cell. e.g.

worksheet name: 'TM L 1'
Cell A1 will display: 'Transmission Line 1'

=SUBSTITUTE(RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename"))),"TM L","Transmission Line")

My problem is that when i create a new sheet it evaultes the code; changes the name to, lets say 'Transmission Line 2' however it also changes the name in the previous sheet to be the same as that of the new sheet.

Does anyone have any ideas what can be done to solve this problem?

Thanks


Chris
 
Cribbed from a post of Bob Phillips' in the MS groups. the bit near in the middle from "One Big Problem" will explain why and how to fix:-

Creating Formulae

The answer to this question lies in the 'CELL' worksheet formula. The
following formula gives the full path and name of the current worksheet
=CELL("filename")


This returns a value that will look something like
D:\Bob\My Documents\My Spreadsheets\[Premiership 2003.xls]League Table


To retrieve just the sheet name, or even the workbook name of full path, we
need to parse the result and extract the required value.

1. The workbook path is simply
=LEFT(CELL("filename"),FIND("[",CELL("filename"))-2)
In my example, this returns
D:\Bob\My Documents\My Spreadsheets


2. The Workbook name is
=MID(CELL("filename"),FIND("[",CELL("filename"))+1,FIND("]",CELL("filename")
)-FIND("[",CELL("filename"))-1)
Which returns
Premiership 2003.xls


3. The sheet name is
=MID(CELL("filename"),FIND("]",CELL("filename"))+1,255)
This final formula returns
League Table


So there we have it, 3 formulae to get the workbook path, the workbook name
and the worksheet name into a worksheet cell.

Restrictions
This technique only works for workbooks that have been saved, at least once.

One Big Problem
There is one major problem with formula 3, the worksheet name. Try this to
highlight the problem.

a.. Enter the formula in a cell on Sheet1, say A1. As expected, you will
see the value 'Sheet1' in the cell
b.. Then enter the same formula in A1 on Sheet2. Again, as expected, you
will see the value 'Sheet2' in A1
c.. Go back to Sheet1, A1 now says Sheet2

The problem here is that every time worksheet recalculation takes place,
each instance of the formula resolves itself to the active worksheet, not
the worksheet that the instance is necessarily on. Fortunately, this is
simply resolved by adding a reference to a cell, any cell, to the formula,
and this anchors the formula to the worksheet it is on. So, the worksheet
name formula then becomes
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)


It matters not which cell is used, it is only a cell reference that is
needed to anchor the formula.

Making a Generic Routine
Since using the worksheet name technique, it occurred to me that it would be
useful to have it available to any workbook that I opened. This seemed easy
enough to do, just create a named range with the above formula in the new
workbook template (Book.let in the XLStart directory), using the following
steps:

a.. goto menu Insert>Name>Define ...
b.. add this value to the 'Names In Workbook' input box sh.name
c.. add this formula to the 'Refers To:' input box
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)
Thus, every time a workbook was 'NEWed', the workbook would have a workbook
name that could be used to get the worksheet name (after it has been saved
of course), simply by putting the formula =sh.name in a cell.

Unfortunately, this doesn't quite work as it should. As before, the value on
each worksheet is always the same, even though we added the cell reference.
Unlike before, the value does not get reset every recalculation, but is
assigned the name of the worksheet that was active when the name was
defined, and only that name. This is due to the fact that when a workbook
name is defined, Excel prefixes any range reference with the name of active
worksheet. For instance, if 'Sheet1' was active when the name sh.name was
defined, the formula would finish up as
=MID(CELL("filename",Sheet1!A1),FIND("]",CELL("filename",Sheet1!A1))+1,255)
thereby negating the effect of adding the cell reference.


What we need to do is create a formula that prefixes the worksheet, but no
particular worksheet, so that we have a variable workbook name that enables
the formula to work correctly in each worksheet, but prevents Excel from
adding the active worksheet name as a prefix. Fortunately, this can be
achieved by simply adding a worksheet delimiter, namely the '!'. Thus the
formula becomes
=MID(CELL("filename",!A1),FIND("]",CELL("filename",!A1))+1,255)
Now, =sh.name returns Sheet1 on Sheet1, and Sheet2 on Sheet2.

Thus, we now have a fully flexible, domprehensive routine to return the
worksheet name of any worksheet, in any newly created workbook.

And Finally ...
In my base workbook template, I have defined a number of workbook names that
incrementally build on the basic CELL formula. I list these below, and leave
you to work them out, and use or not use as you see fit. file.full
=CELL("filename",!$A$1)
file.fullname =LEFT(file.full,FIND("]",file.full))
file.name
=MID(file.fullname,FIND("[",file.fullname)+1,LEN(file.fullname)-FIND("[",fil
e.fullname)-1)
file.dir =LEFT(file.full,FIND("[",file.full)-1)
file.sheet.name =MID(file.full,FIND("]",file.full)+1,255)


Regards
Ken................


----------------------------------------------------------------------------
[peace]It's easier to beg forgiveness than ask permission[2thumbsup]

----------------------------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top