I have reduced my problem to the following test case:
-
create a new workbook;
-
enter a constant value, e.g.
123intoSheet1!A1; -
define a name, e.g.
foo, that refers to the formula=CHOOSE(!$A$1, Sheet1!$A$1); -
enter the constant value
1intoSheet2!A1; -
enter the formula
=foointo some other cell onSheet2, e.g.Sheet2!B1: observe that, as expected, the result is the value that was entered intoSheet1!A1in step 2 above; -
create then run a VBA procedure containing the following code:
Sheets("Sheet1").Outline.ShowLevels 1
You will notice that the cell from step 5 now contains a #VALUE! error.
Moreover, a simple sheet recalculation (whether using the F9 key or the Application.Calculate method) does not resolve the problem: one must instead perform either a full recalculation from VBA (using the Application.CalculateFull method) or else a full rebuild from the interactive UI (using the CTRL+ALT+SHIFT+F9 key combination).
Through trial-and-error, I have ascertained that for this condition to arise:
-
the
CHOOSE()index argument must involve a relative-sheet cell reference (not constants or absolute-sheet references); -
the
CHOOSE()value argument that is being indexed must involve a reference to another sheet; -
the change of displayed outline level must arise from within a VBA procedure (not from the outline controls in the interactive UI or the VBA Immediate Window); and
-
the
ShowLevelsmethod call (whose arguments are irrelevant) must be applied to a sheet that is referenced amongst any of the value (though not index) arguments toCHOOSE().
What’s going on?
I would very much like to collapse a worksheet that I am referencing amongst the value arguments to CHOOSE() to its highest outline level without triggering this error, as a full recalculation of my actual workbook (still only a few seconds) is undesirable from a UX standpoint.
Suggestions for a workaround (whilst still using a defined name containing the CHOOSE() function together with a relative-sheet index argument) would be most welcome!
My platform: Excel 2010 (14.0.6123.5001, 32-bit) on Windows 7 Home Premium (SP1, 64-bit).
The problem is with your named formula:
=CHOOSE(!$A$1, Sheet1!$A$1), specifically the!A1The leading
!is invalid (without a preceeding sheet name, egSheet1!$A$1is valid). Just specify a sheet and your problem goes away.I suspect this may not satisfy you, depending on why you used
!A1in the first place. If it’s that you want=footo use an index value fromA1on the sheet the formula=foois placed on useINDIRECT("A1")instead of!A1BTW I think your may have found a bug, or at least undefned behaviour, in that the formula
=CHOOSE(!$A$1, Sheet1!$A$1)is invalid and should always return a#Valueerror.