Professional Documents
Culture Documents
We’ve learnt quite a bit about the intricacies of Excel; this page documents some of our more unusual
findings. These comments refer to Excel 2007 unless we state otherwise.
Named Ranges can contain cells from multiple sheets, but Ranges cannot
The built in Name Manager allows named ranges to be created that contain cells from multiple sheets.
However, this “named range” cannot be turned into a range because a range must old only cells from a
single sheet. As a consequence, all your decision cells must be on the same sheet (which does not
have to be the active sheet).
Solver stores its integer tolerance in a name solver_tol, with a value such as 0.05 meaning a 5%
tolerance. Like all names, this is simply stored as a string that is NOT affected by regional settings, and
so in VBA this must be interpreted using the Val() function which always uses the full stop, and not the
comma, as the decimal point. We didn’t get this right in OpenSolver until version 1.3beta.
You can see this if you switch your regional settings to French, use Excel’s Name Manager to define a
value such as Test = 0,25 (ie one quarter), and then in VBA’s immediate window type ? Range(“Test”).
The output is 0.25.
Excel is consistent in this regard in that formulae are always stored using English settings. So, if if 1,23
is entered into A1 on a French system, ? Range(“A1″).value gives 1,23, but ? Range(“A1″).Formula
gives 1.23.
VBA, however, is a bit more tricky. If you simply assign a string to a double in VBA, then that string is
interpreted using the current locale, and so using this approach to get constants from names does not
work. This means that this otherw ise excellent code by Chip Pearson does not work on non-English
systems. See OpenSolver’s code for an updated version.
? Evaluate(“1,3+1,6″)
Surprisingly, even if the system settings are set to French, this fails. However, typing
? Evaluate(“1.3+1.6″)
on a French system gives “2,9″ (with a comma) as the output, showing again that formulae must always
be internally in English. Fortunately, we use Evaluate to calculate values for formulae stored in
defined names (which are always stored in US format), so this all “just works” for us.
RefEdit Control
To let the user select a range, we use either an InputBox or a RefEdit control. Both of these have bugs
which make them challenging to use! For the input box, we note:
It may be tempting to use type 8 to indicate a range object, but there is an obscure glitch that causes it
to fail when the worksheet contains conditional formatting conditions that use formulas in their
definitions. Fortunately using Type 0 to specify a formula works just fine. (From here)
The RefEdit control has its own problem detailed here and here.
To let the user select cells on the spreadsheet, we now use the RefEdit control. This has caused us
more grief than anything else! We believe the fundamental issue is that a RefEdit control implements
its own simplified event loop whenever it has the focus, and so you should not do anything
complicated while this RefEdit event loop is running. For example, closing a form or doing
Application.Calculate within this event loop can crash Excel. Note also that VBA breakpoints are all
ignored within this loop.
To help avoid the RefEdit bugs, we suggest you look at the Peltier Tech Blog which notes that
The RefEdit control has some strange and wonderful idiosyncrasies which must be considered when
designing a dialog. There are (at least) four issues that are important to keep in mind.
RefEdits must be placed directly on the UserForm itself. If you put a RefEdit in a frame or on a
multipage, strange things will happen, including bizarre Excel crashes.
RefEdits must not be used on modeless forms. RefEdits on modeless forms will result in bizarre
Excel crashes.
RefEdit event procedures should be avoided. RefEdit events do not behave reliably, and they
may result in VBA errors which are difficult to debug.
References to RefEdits must be removed. When a UserForm is added to a VB project, Excel adds
a reference to the Microsoft Forms 2.0 Object Library. This reference is required for proper
operation of the UserForms in your project. To see what references your project has, select the
project in the Project Explorer, then select References from the Tools menu. [We have not
needed to do this.]
When a RefEdit is added to a UserForm, Excel sometimes adds a reference to the Ref Edit
Control. This was fine in earlier versions of Excel, but there appear to be incompatibilities in
recent versions of the RefEdit library. These problems often are associated with an Unspecified
but Painfully Frustrating Error.
It is also helpful to process the workbook or add-in’s code through an earlier version of Excel. I
use a VM with Office 2000 installed on it for this purpose. Open Excel 2000, open the workbook
or add-in, run Rob Bovey’s free Code Cleaner application, then compile the code and save the
file.
It may also be necessary to remove RefEdit.exd files from a user’s computer if problems occur
and unchecking the reference doesn’t fix them. these exd files are temporary files that contain
instructions Excel uses to build RefEdits, and the exd may conflict with the RefEdits in the
workbook. If this doesn’t work, the next step is to run Detect and Repair on Excel, then reinstall
Office, then sacrifice the computer at the next full moon.
If you are enabling/disabling controls on a form, make sure that the last item you enable is not a
RefEdit. Our code that was doing this would crash Excel on the second time around when it
called Application.Calculate.
In OpenSolver, Excel seems to crash most often when users press <return> instead of clicking on a
button (eg on the Continue button), or when the user presses <escape> instead of clicking Cancel
(which we now disable). To try to catch these, we have made sure that the form is not closed while a
RefEdit control has the focus, and put in lots of DoEvents around anything involving a RefEdit
control. These checks seem to work, mostly, but the RefEdit is still somewhat unreliable.
In version 1.3beta, we no longer associate pressing the return key with the next Continue button. When
this was allowed (eg in version 1.2 beta), selecting a blank objective cell, and then pressing return
caused an error dialog to appear (even tho’ there was an On Error top catch this error) and also caused
a floating RefEdit dialog to show; this sometimes then resulted in crashes. Debugging this was difficult
as breakpoints in this code were ignored, perhaps because RefEdit was confusing the debugger. (I
suspect that RefEdit runs its own event loop to do its clever stuff, and this causes all these problems.)
We do, however, still allow Escape to ‘press’ the Cancel button (via Cancel=true for this button); this
code again runs without stopping at breakpoints, but does not seem to cause crashes.
Note also that, as detailed here, RefEdit does not fire the AfterUpdate and BeforeUpdate events, and
setting the .Font property of cells (eg to colour selected cells blue) on the Enter or Exit events crashes
Excel when the focus leaves the RefEdit control. Both the .Text and .Range properties of the RefEdit
give the range selected as a string.
See also this know ledge base article for Excel 2002: XL2002: VBA Error on Macro Startup If You
Open Multiple Excel Instances with RefEdit Control.
We have found that the VBA debugger is not good at tracking events associated with RefEdit’s. For
example, RefEdit events may fire and run ok, but without triggering any breakpoints set in the event
handler. We have seen errors get thrown (and a dialog appear on the screen), even though our event
handler has an On Error handlers; On Error handlers (and errors in general) are perhaps best avoided.
(Given the tricks the RefEdits do, it is likely that they run their own main event loop which allows them
to process clicks in Excel windows. We suspect that differences between this main event loop standard
VBA main event loop leads to things operating differently in subtle but dangerous ways!)
This page suggests that you trace the refedit’s exit event (by debug.print, not breakpoints), and states
that it MUST occur before you hide or close your userform. The following code is given to help ensure
this:
The source for a C#.net RefEdit replacement is available on Gabhan Berry’s MSDN Excel
Programming blog.