+ Reply to Thread
Results 1 to 8 of 8

Single command to toggle a Boolean?

  1. #1
    Ed
    Guest

    Single command to toggle a Boolean?

    In Excel 2000 VBA, is there one single command to toggle a Boolean - that
    is, in whatever state it's in, change it to the opposite? I know it can be
    done with an If / Else - I was just wondering if there was a singe-line way
    to do it.

    Ed



  2. #2
    Bill Martin
    Guest

    Re: Single command to toggle a Boolean?

    Ed wrote:
    > In Excel 2000 VBA, is there one single command to toggle a Boolean - that
    > is, in whatever state it's in, change it to the opposite? I know it can be
    > done with an If / Else - I was just wondering if there was a singe-line way
    > to do it.
    >
    > Ed
    >
    >


    ----------------

    Dim Flag as Boolean
    Flag = not(Flag)

    Bill

  3. #3
    Chip Pearson
    Guest

    Re: Single command to toggle a Boolean?

    Ed,

    Use the Not operator. This will switch a boolean to its opposite.
    E.g,

    Dim Var As Boolean Dim Var As Boolean
    Var = Not Var


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com



    "Ed" <ed_millis@NO_SPAM.yahoo.com> wrote in message
    news:[email protected]...
    > In Excel 2000 VBA, is there one single command to toggle a
    > Boolean - that
    > is, in whatever state it's in, change it to the opposite? I
    > know it can be
    > done with an If / Else - I was just wondering if there was a
    > singe-line way
    > to do it.
    >
    > Ed
    >
    >




  4. #4
    Chip Pearson
    Guest

    Re: Single command to toggle a Boolean?

    I have no idea how I got two Dim statements in the code. The
    code should have only one Dim Statement.

    Dim Var As Boolean
    Var = Not Var


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com



    "Chip Pearson" <[email protected]> wrote in message
    news:[email protected]...
    > Ed,
    >
    > Use the Not operator. This will switch a boolean to its
    > opposite. E.g,
    >
    > Dim Var As Boolean Dim Var As Boolean
    > Var = Not Var
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    >
    > "Ed" <ed_millis@NO_SPAM.yahoo.com> wrote in message
    > news:[email protected]...
    >> In Excel 2000 VBA, is there one single command to toggle a
    >> Boolean - that
    >> is, in whatever state it's in, change it to the opposite? I
    >> know it can be
    >> done with an If / Else - I was just wondering if there was a
    >> singe-line way
    >> to do it.
    >>
    >> Ed
    >>
    >>

    >
    >




  5. #5
    Ed
    Guest

    Re: Single command to toggle a Boolean?

    Thanks, Chip.

    "Chip Pearson" <[email protected]> wrote in message
    news:[email protected]...
    > I have no idea how I got two Dim statements in the code. The
    > code should have only one Dim Statement.
    >
    > Dim Var As Boolean
    > Var = Not Var
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    >
    > "Chip Pearson" <[email protected]> wrote in message
    > news:[email protected]...
    > > Ed,
    > >
    > > Use the Not operator. This will switch a boolean to its
    > > opposite. E.g,
    > >
    > > Dim Var As Boolean Dim Var As Boolean
    > > Var = Not Var
    > >
    > >
    > > --
    > > Cordially,
    > > Chip Pearson
    > > Microsoft MVP - Excel
    > > Pearson Software Consulting, LLC
    > > www.cpearson.com
    > >
    > >
    > >
    > > "Ed" <ed_millis@NO_SPAM.yahoo.com> wrote in message
    > > news:[email protected]...
    > >> In Excel 2000 VBA, is there one single command to toggle a
    > >> Boolean - that
    > >> is, in whatever state it's in, change it to the opposite? I
    > >> know it can be
    > >> done with an If / Else - I was just wondering if there was a
    > >> singe-line way
    > >> to do it.
    > >>
    > >> Ed
    > >>
    > >>

    > >
    > >

    >
    >




  6. #6
    Ed
    Guest

    Re: Single command to toggle a Boolean?

    Thanks, Bill.

    "Bill Martin" <[email protected]> wrote in message
    news:[email protected]...
    > Ed wrote:
    > > In Excel 2000 VBA, is there one single command to toggle a Boolean -

    that
    > > is, in whatever state it's in, change it to the opposite? I know it can

    be
    > > done with an If / Else - I was just wondering if there was a singe-line

    way
    > > to do it.
    > >
    > > Ed
    > >
    > >

    >
    > ----------------
    >
    > Dim Flag as Boolean
    > Flag = not(Flag)
    >
    > Bill




  7. #7
    Registered User
    Join Date
    12-04-2006
    Posts
    11

    Re: Single command to toggle a Boolean?

    14 years ago, but the issue still.
    Excel VBA

    A = 1
    A = NOT A


    A results in -2

    A = NOT A


    A results in +1, and keeps flipping between +1 and -2.
    Why is that? Why not flips between 1 and 0 ?

    I use

    A = ABS(A-1) to flip between 1 and 0 faster, but thought NOT could do it too.

    using XOR also gives me what I need,
    A = A XOR 1
    Flips between 1 and 0 nicely, but still using one extra argument than NOT.

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Single command to toggle a Boolean?

    Quote Originally Posted by wagnerlip View Post
    14 years ago, but the issue still.
    Excel VBA

    A = 1
    A = NOT A


    A results in -2

    A = NOT A


    A results in +1, and keeps flipping between +1 and -2.
    Why is that? Why not flips between 1 and 0 ?

    I use

    A = ABS(A-1) to flip between 1 and 0 faster, but thought NOT could do it too.

    using XOR also gives me what I need,
    A = A XOR 1
    Flips between 1 and 0 nicely, but still using one extra argument than NOT.
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1