Use Checkbox Value or Public Variable?

S

Stratuser

When an Excel file with a macro is opened, do all the
forms and the controls on the forms exist before the form
is initialized or called? The reason I am asking is that
I was recently using the value of a form's checkbox
instead of a boolean variable to control an IF statement.
It seems to work just as well either way, but I decided
that it might be dangerous to assume that the form's
checkbox is true or false or even that the form exists
before being called, so I created an explicit boolean
variable and made it public. What's the best practice?
 
B

Bob Phillips

Start,

Don't rely on it. It doesn't exist on the workbook opening, but when you
reference it, it is loaded into memory. Loading initialises all controls,
etc. So a checkbox is initialised to whatever the design sets it at. A
variable is the same problem, re-opening the workbook will initialise.

The safest places are the registry, stored in a file, or a workbook name.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
O

onedaywhen

You can be guaranteed a newly-declared boolean variable will have a
value that coerces to false i.e.

Sub test()
Dim blnTest As Boolean
Debug.Print blnTest ' returns false
Debug.Print blnTest = Empty ' returns true
Debug.Print IsEmpty(blnTest) ' returns false
End Sub

But best practise is to never use a *public* variable.
 
Top