toggle between 0 and 1

Is there a binary operator in Visual Basic
that will toggle a variable (initially set to 0)
between 0 and 1???

I know I could use a bunch of if-then statements
to toggle my variable between 0 and 1, but isnt
there a binary operator that can set my variable
to 0 if it equals 1, and set it to 1 if it equals 0???

thankx

0
Robert
12/30/2009 8:58:12 AM
excel.programming 6508 articles. 2 followers. Follow

9 Replies
1118 Views

Similar Articles

[PageSpeed] 7

Assuming your variable's name is ZeroOne, executing this statement will 
toggle it from 1 to 0 or 0 to 1 depending on its current value...

ZeroOne = 1 - ZeroOne

-- 
Rick (MVP - Excel)


"Robert Crandal" <nobody@gmail.com> wrote in message 
news:GCE_m.9296$pA1.5275@newsfe17.iad...
> Is there a binary operator in Visual Basic
> that will toggle a variable (initially set to 0)
> between 0 and 1???
>
> I know I could use a bunch of if-then statements
> to toggle my variable between 0 and 1, but isnt
> there a binary operator that can set my variable
> to 0 if it equals 1, and set it to 1 if it equals 0???
>
> thankx
> 

0
Rick
12/30/2009 9:23:40 AM
You can use the NOT operator as shown below

Dim blnToggle As Boolean

'Reset
blnToggle = Not blnToggle
MsgBox -blnToggle

'Reset again
blnToggle = Not blnToggle
MsgBox -blnToggle


-- 
Jacob


"Robert Crandal" wrote:

> Is there a binary operator in Visual Basic
> that will toggle a variable (initially set to 0)
> between 0 and 1???
> 
> I know I could use a bunch of if-then statements
> to toggle my variable between 0 and 1, but isnt
> there a binary operator that can set my variable
> to 0 if it equals 1, and set it to 1 if it equals 0???
> 
> thankx
> 
> .
> 
0
Utf
12/30/2009 9:40:01 AM
Another way

x = x Xor 1

Regards,
Peter T


"Robert Crandal" <nobody@gmail.com> wrote in message 
news:GCE_m.9296$pA1.5275@newsfe17.iad...
> Is there a binary operator in Visual Basic
> that will toggle a variable (initially set to 0)
> between 0 and 1???
>
> I know I could use a bunch of if-then statements
> to toggle my variable between 0 and 1, but isnt
> there a binary operator that can set my variable
> to 0 if it equals 1, and set it to 1 if it equals 0???
>
> thankx
> 


0
Peter
12/30/2009 10:01:06 AM
Hello Jacob,

That toggles between -1 and 0, I think...

Regards,
Bernd
0
Bernd
12/30/2009 11:34:41 AM
Hello Jacob,

Sorry, did not see your "-" directly after the MSGBOX ...

Regards,
Bernd
0
Bernd
12/30/2009 11:36:00 AM
Others have shown you other ways to toggle between 0 and 1 (although I kind 
of think the subtraction method I proposed is the easiest to understand and 
implement). And this method lends itself to generalizing as well. To toggle 
a variable V between any two numbers N1 and N2, you would just use this line 
of code...

V = N1 + N2 - V

Think about it... if V equals N1, then the above line returns N2 and if V 
equals N2, then the above line returns N1... simple, right? This is exactly 
what I used for my initial response to you where N1 equaled 0 and N2 equaled 
1. Now, if one of your numbers is not zero, then there might be a little 
extra work involved to implement this (same problem if you use If..Then 
statements as well)... all numeric variables start off defaulted to zero, so 
you can use the above line immediately when one of the numbers is zero; but 
when both numbers are not zero, then you have to get your variable V 
initialized to one of the numbers before you can start toggling them.

-- 
Rick (MVP - Excel)


"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message 
news:O5OgEHTiKHA.6096@TK2MSFTNGP02.phx.gbl...
> Assuming your variable's name is ZeroOne, executing this statement will 
> toggle it from 1 to 0 or 0 to 1 depending on its current value...
>
> ZeroOne = 1 - ZeroOne
>
> -- 
> Rick (MVP - Excel)
>
>
> "Robert Crandal" <nobody@gmail.com> wrote in message 
> news:GCE_m.9296$pA1.5275@newsfe17.iad...
>> Is there a binary operator in Visual Basic
>> that will toggle a variable (initially set to 0)
>> between 0 and 1???
>>
>> I know I could use a bunch of if-then statements
>> to toggle my variable between 0 and 1, but isnt
>> there a binary operator that can set my variable
>> to 0 if it equals 1, and set it to 1 if it equals 0???
>>
>> thankx
>>
> 

0
Rick
12/30/2009 6:40:32 PM
Hi Rick.  I usually do my programming with C++ or Intel
assembly language, which I know have bit flipping operators.
I was just curious if VBA had something similar.

However, your solution is very interesting because it can
be generalized.   However, if I wanted to toggle between
5 different values, it would involve a "little extra work"
of the if-then statements right??   In cases like that, would
it be better to use a "select-case" statement??

BTW, I always appreciate your expert advice.  thank you!


"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message 
news:%23gDEP%23XiKHA.5524@TK2MSFTNGP06.phx.gbl...
> Others have shown you other ways to toggle between 0 and 1 (although I 
> kind of think the subtraction method I proposed is the easiest to 
> understand and implement). And this method lends itself to generalizing as 
> well. To toggle a variable V between any two numbers N1 and N2, you would 
> just use this line of code...
>
> V = N1 + N2 - V
>
> Think about it... if V equals N1, then the above line returns N2 and if V 
> equals N2, then the above line returns N1... simple, right? This is 
> exactly what I used for my initial response to you where N1 equaled 0 and 
> N2 equaled 1. Now, if one of your numbers is not zero, then there might be 
> a little extra work involved to implement this (same problem if you use 
> If..Then statements as well)... all numeric variables start off defaulted 
> to zero, so you can use the above line immediately when one of the numbers 
> is zero; but when both numbers are not zero, then you have to get your 
> variable V initialized to one of the numbers before you can start toggling 
> them.
>
> -- 
> Rick (MVP - Excel)
>

0
Robert
12/30/2009 8:40:55 PM
Hello,

In general - for more than 2 values - I would load all different
values into an array:
Dim i As Long, n As Long
ReDim dA(0 to n-1) As Double
dA(0) = 1# 'First value
....
dA(n-1) = n 'n-th value

i = 0

Now toggle with
i = (i+1) mod n
and use dA(i)

Regards,
Bernd
0
Bernd
12/31/2009 3:42:52 AM
For all of the following discussion, let's say the 5 numbers to toggle 
between in the listed order are these... 5, 123, 73, 10, 9 and the toggle 
variable is named V (where V is assumed to be initialized to one of the 
variables in the list).

1. If..Then blocking is not really that bad to implement...

      If V = 5 Then
           V = 123
      ElseIf V = 123 Then
           V = 73
      ElseIf V = 73 Then
           V = 10
      ElseIf V = 10 Then
           V = 9
      ElseIf V = 9 Then
           V = 5
      End If

2. Select..Case blocking can be made more compact...

      Select Case V
           Case 5: V = 123
           Case 123: V = 73
           Case 73: V = 10
           Case 10: V = 9
           Case 9: V = 5
      End Select

3. Bernd's array method is also a feasible way to go...

      ' These statements go in the module's (General)(Declarations) section
      Option Base 0
      Dim Arr As Variant
      Static Index As Long

      ' These statements go in your toggle subroutine
      Arr = Array(5, 123, 73, 10, 9)
      Index = (Index + 1) Mod UBound(Arr)
      V = Arr(Index)

4. A one-liner method involves setting up a string with all the numbers
   expanded to the same number of digits as the longest number in the
   toggle sequence (using leading zeroes) with any non-digit character
   between them (if that number has a decimal point in it, consider it
   as a digit). So, for our example, this is how to set it up so the
   toggle subroutine is a one-liner...

      ' Declare V as a Long or Double depending on what the actual values
      ' are and put it in the module's (General)(Declarations) section
      Dim V As Long

      ' Initialize a toggle sequence constant named TS for this example
      ' in the code module's (General)(Declarations) section; note that
      ' the first number is repeated at the end.
      Const TS As String = "005,123,073,010,009,005"

      ' This one-liner statement goes in your toggle subroutine
      V = Mid(TS, InStr(TS, Format(V, "000")) + 4, 3)

      The value being added (4 in this case) is one greater than the
      number of digits in the largest toggle sequence value (123 has
      3 digits, so we add 4... the extra 1 is so we will skip over the
      non-digit delimiter (a comma in my example), the 3 (number of
      characters returned from the Mid function call) is equal to the
      number of digits in the largest toggle sequence value.

-- 
Rick (MVP - Excel)



"Robert Crandal" <nobody@gmail.com> wrote in message 
news:tVO_m.5802$YG1.1347@newsfe14.iad...
> Hi Rick.  I usually do my programming with C++ or Intel
> assembly language, which I know have bit flipping operators.
> I was just curious if VBA had something similar.
>
> However, your solution is very interesting because it can
> be generalized.   However, if I wanted to toggle between
> 5 different values, it would involve a "little extra work"
> of the if-then statements right??   In cases like that, would
> it be better to use a "select-case" statement??
>
> BTW, I always appreciate your expert advice.  thank you!
>
>
> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message 
> news:%23gDEP%23XiKHA.5524@TK2MSFTNGP06.phx.gbl...
>> Others have shown you other ways to toggle between 0 and 1 (although I 
>> kind of think the subtraction method I proposed is the easiest to 
>> understand and implement). And this method lends itself to generalizing 
>> as well. To toggle a variable V between any two numbers N1 and N2, you 
>> would just use this line of code...
>>
>> V = N1 + N2 - V
>>
>> Think about it... if V equals N1, then the above line returns N2 and if V 
>> equals N2, then the above line returns N1... simple, right? This is 
>> exactly what I used for my initial response to you where N1 equaled 0 and 
>> N2 equaled 1. Now, if one of your numbers is not zero, then there might 
>> be a little extra work involved to implement this (same problem if you 
>> use If..Then statements as well)... all numeric variables start off 
>> defaulted to zero, so you can use the above line immediately when one of 
>> the numbers is zero; but when both numbers are not zero, then you have to 
>> get your variable V initialized to one of the numbers before you can 
>> start toggling them.
>>
>> -- 
>> Rick (MVP - Excel)
>>
> 

0
Rick
12/31/2009 6:40:13 AM
Reply:

Similar Artilces:

offline sources in DPM 2007 SP1 ver 2.0.8851.0
I'm looking for some help troubleshooting an issue when backing up my Exchange databases where the error I get is "The operation failed for Storage group on exchange server because the data source is not available. ID 30169 Details: Unknown error Then the alert goes on to tell me to verify that the data source is online, is not in a restoring state...etc. I'm at a lost to believe that one of my exchange databases is offline because we have plenty of other monitors that would be letting us know. How do I go about troubleshooting this? Is there a latency issue...

Upgraded Sales for Outlook 1.2 to 3.0 but lost all my previous ema
Hi, I have successfully upgraded SFO 1.2 to 3.0 but lost all my previous emails on MSCRM.pst. Has anyone out there had experienced the same thing. Is there a way to recover back all my old emails? I think the chances are slim as the upgrade process has overwritten the old MSCRM.pst with an empty one. It would be a pain in the next if i can't recover the old emails. Any advise is welcome. Thanks. Kim. ...

Toggle Button #3
Hi, I am constructing a spreadsheet, which has several ToggleButtons. Everything is working fine, except for the one toggle button, which has its TripleState, set to be TRUE. The offending formula (of which, the final argument is troublesome), appears below. IF(A1=TRUE,"The Maximum Amount",IF(A1=FALSE,"The Median Amount",IF(A1=#N/A,"The Minimum Amount"))) Where am I going wrong? Regards, Mick2 -- mick2 ------------------------------------------------------------------------ mick2's Profile: http://www.excelforum.com/member.php?action=getinfo&useri...

Howto upgrade Pos 2.0 to Pos 2009
Hi all, I have a friend who purchased the retail version of MS Pos 2.0 at CDW. Till now Pos is not yet implemented (time/people issues). Now he wants to make a new start but with Pos 2009. With the purchase of Pos 2.0, can he upgrade to Pos 2009 for free? if yes: will there be an online activation again? if not: how can the upgrade be done? is there an upgrade package which can be purchased from a MBS partner? any idea, anyone? Many thanks in advance This is a multi-part message in MIME format. ------=_NextPart_000_012F_01CA4396.EA134FD0 Content-Type: text/plain; charset="u...

Business Portal 2.0 to 2.5 Upgrade Failure
Heads up for those running Business Portal 2.0 with SP1. If you plan on upgrading to Great Plains 8.0 and Business Portal 2.5, make sure you have the Business Portal 2.5 with Feature Pack CD which is "supposed" to ship this week from what I've been told. Once you install Business Portal 2.0 SP1, you will not be able to upgrade to Business Portal 2.5 using the original BP25 CD. BP20 SP1 sets the build number at 859, and the original BP25 CD is expecting to see 823. As soon as the upgrade goes to load data, the upgrade will die and you will see the following error in the...

GP 9.0 SP2
Do I absolutely need SP2 before processing 2007 pay? I am still waiting for compatability confirmation from some of our 3rd party add-on product vendors and then have to test all that, along with our home-grown SQL triggers, VBA, and mods to ensure that it all hangs together well! I can see this dragging past January 3rd, when we have to process our first payroll to be paid in 2007. Are there options that are maybe not quite "official Microsoft", but work nonetheless? Thanks for any insight anyone can provide. ...

Toggling Fullscreen
How can i hide the toolbar on top of my dialog or window in and show it again ? I am asking this to make a fullscreen toggling. Thank you very much. If your window is a dialog : CToolbar::ShowWindow(SW_HIDE) can do that. If your window is a CFrameWnd, ShowControlBar which will take care of expanding the childs windows in the free space. You may also need to switch on the WS_POPUP style if you want to hide the title bar too. Emmanuel On Jul 2, 9:14 am, Ak n =D6Cal <Akn...@discussions.microsoft.com> wrote: > How can i hide the toolbar on top of my dialog or window in and show it a...

New license key for upgrades from 3.0 to 4.0
Is there a process for obtaining a new license key that will be accepted during an upgrade from 3.0 to 4.0. My current 3.0 license is not being accepted! Thanks in advance, ~Maria Trial license keys are provided inthe readme that comes with the downloads. The rpocess for getting the "real" keys has not been announced. -- Matt Parks MVP - Dynamics CRM "Maria Greendyk" <MariaGreendyk@discussions.microsoft.com> wrote in message news:D5CAE82E-A779-4FFD-9D72-3E0E02D3580C@microsoft.com... Is there a process for obtaining a new license key that will be accepted d...

CRM 3.0 Outlook Laptop Client Crashes
We are evaluating CRM in connection with the Ourlook Laptop Client. First it worked quite fine, after a while the Client cant go offline anymore. Syncronisation with Outlook works fine, but as I try to go offline, the sync proces workes fine for the first moment, then it tries to connect to the local Offline DB and after a while it crashes. In the event log I get the following entry: An error occurred during Offline Synchronization. Try going offline again, or restart Microsoft Outlook. DoSyncMetabaseIds() failed with exception Microsoft.Crm.CrmException: Failed to start or connect t...

'1 item remaining' message in the status bar
When i try to access some of the pages in my application sometimes the browser status will say '1 item remaining' in the status bar indefinitely. Occasionally the page will not re-render. Clicking on the page or refreshing the page again also removes the '1 item remaining' message in the status bar. This happens only with IE7. This is not an issue in Firefox, Opera, Chrome, or Safari. It's an 'issue' for every web browser; to my knowledge, only Internet Explorer displays a message in the Status Bar that tells you about it. These days, a single web...

Multi-tenancy
Has anyone attempted to merge two CRM 3.0 instances to a single multi-tenancy CRM 4.0 instance? I can see how the first upgrade would work to create the 4.0 instance, but not how to merge in subsequent organizations. You have to upgrade them seperately and then use the Import Organization feature (the new redeployment tool). If both use the same SQL server, then you will need to do some extra steps as the second upgrade will fail since the main cofig DB already exists. -- Matt Parks MVP - Dynamics CRM "Duane Robinson" <DuaneRobinson@discussions.microsoft.com> wrote ...

Dynamics.EXE process hangs in 8.0
Occassionally one of our IM jobs fails and we end up with multiple copies of Dynamics.exe running in Windows Task manager on the server (IM runs on the server box). We use standard macros to log in. In 6.0 when this happened we could delete the IM log in if necessary from the Activity window and go to the Windows Task Manager and kill the Dynamics.exe process. This doesn't work in 8.0 with Windows 2003 server. If Dynamics.exe hangs, it requires a reboot. Is there a workaround or some programmatic way of killing this process? ...

Toggle?
Is there a keyboard shortcut to toggle between display formulas and the text view? Thanks. see answer in worksheet.functions - not generally necessary to multi-post questions "OhMarty" <ohmarty@tampabay.rr.com> wrote in message news:uQGFoiYdEHA.3476@tk2msftngp13.phx.gbl... > Is there a keyboard shortcut to toggle between display formulas and the text > view? Thanks. > > Sorry, I had thought I had posted in wrong group, so I reposted. I got the right answer. Thanks to everyone. "JulieD" <JulieD@hctsReMoVeThIs.net.au> wrote in message news:OVdLK...

Crystal Enhancements for 1.2
Hi, I am installing the Crystal Enhancements for CRM 1.2. I get an error saying I have to have Crystal 9.2 installed. Right now I have 9.0 installed. Is the update on the CRM CD? There is an .msi file under the Crystal folder but I am not sure what this file is. Thanks. Alan Never mind. Got the answer. "Alan Whitehouse" <awhitehouse@NOSPAM.tgo.ca> wrote in message news:%23vRoOmKyDHA.1736@TK2MSFTNGP09.phx.gbl... > Hi, > > I am installing the Crystal Enhancements for CRM 1.2. I get an error > saying I have to have Crystal 9.2 installed. Right now I...

Questions about MS CRM 3.0
Hi, a) We have different external and internal email addresses. Will the CRM system be able to reconcile/deal with these? (for example, my external address is abe.lincoln@foo.com, but my internal address is abe.lincoln@ny.foo.com) b) Will MS CRM be able to track more than one email address of a client? i.e. the contact has a work email and a home email. c) how does the CRM plugin for exchange work? Does it involve some sort of exchage rules? If it is rules, is there anyway for users to prevent the plugin from working by modifying/deleting the rules? d) if I send a CRM email, is SMTP i...

CRM 1.2 Installation failed
Hi, I got an error message trying to install CRM 1.2 on a fresh installed Windows 2003 Server with SQL 2000. The error message is as followed: Error 1713. Microsoft CRM Server cannot install one of its requred products. Contact yur technical support group. What does the error message mean? Anyone there has encountered the same problem? The AD, SQL ,IIS are all in good working order. I could not think of anything that could cause the problem. I am using the Adventure Works Cyclte sample key. Thanks in advance. Alex > I got an error message trying to install CRM 1.2 on a > fresh ...

Toggle
Please all MS Guru's Where I can find on Toolbox for design Windows.Form Application Toggle control ? How Add or something else. Thx kresimir ivkovic wrote: > Please all MS Guru's > > Where I can find on Toolbox for design Windows.Form Application > Toggle control ? > How Add or something else. I'm not sure what you mean. But maybe you're asking for the Checkbox control? It should be in the Toolbox, labeled "Checkbox". ...

Connection goes down in CRM 3.0
Hi, I´m having a problem with the CRM connection. It doesn´t matter if i´m accessing the CRM via iexplorer or via Outlook Client. It seems like it gives some sort of time out session. I ask something and then a loggin box shows up and I have to close the crm (outlook or the browser) in order to restore the connection. Any ideas to solve this? -- Regards, Ricardo ...

Great Plains 8.0 to 9.0 upgrade problem
We just upgraded the server successfully, then installed the clients. When we try to login to the client, we're getting: "The database setup has not been completed for Microsoft Dynamics GP. Use Microsoft Dynamics GP Utilities to complete the database setup before starting Microsoft Dynamics GP." Click OK, the message pops up 3 more times. What is the best way to troubleshoot this message? I can't find any reference to this error. We figured out the problem. There were modules installed that our registration keys did not match, so that's why we were getting the...

toggling a chart on and off
Hello! I use Excel 2000. I have a macro that creates a chart. I assigned this macro to a autoshape. When I am done looking at the chart I have to delete it Isn't there a way to make things in such a way that I could just toggl the chart to appear and dissapear? For example with an additional macr or toggle button or something... This is how the macro looks like: Sub Macro1() ' ' Macro1 Macro ' Macro recorded 25.2.2004 by Registered User ' ' Range("B3:J3,B5:J5").Select Range("B5").Activate Charts.Add ActiveChart.ChartType = xlColumnClustered Acti...

Large reports Quit CRM4.0
Trying to run lists of accounts, export to Excel as a static worksheet. For example. . all accounts that begin with "A". . . Gets to about 5 bars, and it quits or times out. Just stops. . ."Back to Query" is available though. Thoughts? Sounds like a job for the IT bunch? Tried clearing the cache. Pierre On Mar 7, 11:13=A0am, Pierre <cow...@aol.com> wrote: > Trying to run lists of accounts, export to Excel as a static > worksheet. For example. . all accounts that begin with "A". . . Gets > to about 5 bars, and it quits or times out. =A0Just stops...

CRM 4.0 Offline Client Installation
Is it possible to specify the location of the CRM 4.0 Offline Client MSDE database during installation? By default it goes to the [User Profile]\Application Data\Microsoft \MSCRM\Data folder but I need to move it outside of Application Data to overcome a restrictive Group Policy. I can do this with a post installation SQLCMD script but it would be nice to be able to configure this at installation time. Thanks in advance Richard ...

16 Ounce White All Purpose Cotton Mop with 1 Inch Headband (10-0095) Category: Wet Mops
List Price:$5.20 Image: http://bestdeallocator.info/image.php?id=B00125PHTQ Best price found: http://bestdeallocator.info/index.php?id=B00125PHTQ Item #: 10-0095. Excellent, all-purpose mop for general cleaning. Looped ends reduce fraying associated with cut-end mops. Universal headband fits both side-loading and clamp-style handles. Customers also search for: Discount 16 Ounce White All Purpose Cotton Mop with 1 Inch Headband, Buy 16 Ounce White All Purpose Cotton Mop with 1 Inch Headband, Wholesale 16 Ounce White All Purpose Cotton Mop with 1 Inch Headband, 086876050103, F116, Wet Mop...

Titan -NDA Restrictions has dropped from October 1
http://www.stunnware.com/crm2/topic.aspx?id=TitanBeta2 I think its time to talk and discuss about the new Functionalities, disappointments and excitements. Regards Aamir Aamir, I have not heard or seen any official announcements from Microsoft yet, so I recommend you wait for now. Michael Cross Navint http://www.navint.com "Aamir" wrote: > http://www.stunnware.com/crm2/topic.aspx?id=TitanBeta2 > > I think its time to talk and discuss about the new Functionalities, > disappointments and excitements. > > Regards > Aamir > > Hi, Well i got i...

0'0'
90...