Consulting

Results 1 to 10 of 10

Thread: Declaring global Boolean variable?

  1. #1
    VBAX Regular
    Joined
    Jul 2011
    Posts
    19
    Location

    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?

    [VBA]Public CheckBoolean1 As Boolean
    Public CheckBoolean2 As Boolean

    CheckBoolean1 = False
    CheckBoolean2 = False[/VBA]

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Set your values in a Module's Sub.

  3. #3
    VBAX Regular
    Joined
    Jul 2011
    Posts
    19
    Location
    So i can't preset my global Boolean values initially? Or is the initial Boolean vlue set to false?

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by imso
    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:
    [VBA]Option Explicit

    Public CheckBoolean1 As Boolean
    Public CheckBoolean2 As Boolean

    Sub exa()
    MsgBox CheckBoolean1
    MsgBox CheckBoolean2
    End Sub[/VBA]

    Hope that helps,

    Mark

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You could always set the variable on the Workbook_Open event if you want to initialise it as True.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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:
    [vba]Option Explicit

    Public CheckBoolean1 As Boolean
    Public CheckBoolean2 As Boolean

    Sub SetDefaults()
    CheckBoolean1 = False
    CheckBoolean2 = False
    End Sub[/vba]
    In the VBE, doubleclick ThisWorkbook object in the Project Explorer and paste. If it is not shown, select View > Property Explorer.
    [vba]Private Sub Workbook_Open()
    SetDefaults
    End Sub[/vba]
    Of course you can run SetDefaults from any routine.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I would explicitly set, even for False. Relying on system behaviour is dangerous IMO.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Jul 2011
    Posts
    19
    Location
    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
    Last edited by imso; 07-25-2011 at 05:55 PM.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes, Workbook_OPen. I mentioned that earlier, Kenneth gave you an example.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Regular
    Joined
    Jul 2011
    Posts
    19
    Location
    Oh I've missed that because i do not know what Kenneth gave me was what i am mentioning above. Thanks Guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •