View Full Version : Declaring global Boolean variable?
Sorry if this might be a amateur question that i'm asking.. May i ask you guys how should i actually declare global Boolean variable and fixing it to false? Or by default Excel VBA already gave it a false value? But to be safe i wanted to know how to declare and fix a value for global Boolean?
I tired this but it kept giving me error?
Public CheckBoolean1 As Boolean
Public CheckBoolean2 As Boolean
CheckBoolean1 = False
CheckBoolean2 = False
Kenneth Hobs
07-24-2011, 06:41 PM
Set your values in a Module's Sub.
So i can't preset my global Boolean values initially? Or is the initial Boolean vlue set to false?
So i can't preset my global Boolean values initially? Or is the initial Boolean vlue set to false?
Yes, a Boolean is initialized as False. You can check this by stepping through the code, or just making yourself a simple temp sub to learn, like:
In a Standard Module:
Option Explicit
Public CheckBoolean1 As Boolean
Public CheckBoolean2 As Boolean
Sub exa()
MsgBox CheckBoolean1
MsgBox CheckBoolean2
End Sub
Hope that helps,
Mark
Bob Phillips
07-24-2011, 11:57 PM
You could always set the variable on the Workbook_Open event if you want to initialise it as True.
Kenneth Hobs
07-25-2011, 05:34 AM
Most likely, you will want to set the defaults on open as xld said. There is no need to do that though if False is what you need.
In a Module paste:
Option Explicit
Public CheckBoolean1 As Boolean
Public CheckBoolean2 As Boolean
Sub SetDefaults()
CheckBoolean1 = False
CheckBoolean2 = False
End Sub
In the VBE, doubleclick ThisWorkbook object in the Project Explorer and paste. If it is not shown, select View > Property Explorer.
Private Sub Workbook_Open()
SetDefaults
End Sub
Of course you can run SetDefaults from any routine.
Bob Phillips
07-25-2011, 07:13 AM
I would explicitly set, even for False. Relying on system behaviour is dangerous IMO.
Hi Guys,
Thanks for your advice but do you guys happen to know is there a function which will auto initialize upon opening the workbook? So that i can set certain value at the beginning?
Regards,
imso
Bob Phillips
07-26-2011, 12:37 AM
Yes, Workbook_OPen. I mentioned that earlier, Kenneth gave you an example.
Oh I've missed that because i do not know what Kenneth gave me was what i am mentioning above. Thanks Guys :)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.