Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 309
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default toggle between 0 and 1

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" wrote in message
...
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default toggle between 0 and 1

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

.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default toggle between 0 and 1

Another way

x = x Xor 1

Regards,
Peter T


"Robert Crandal" wrote in message
...
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



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 806
Default toggle between 0 and 1

Hello Jacob,

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

Regards,
Bernd


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 806
Default toggle between 0 and 1

Hello Jacob,

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

Regards,
Bernd
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default toggle between 0 and 1

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" wrote in message
...
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" wrote in message
...
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



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 309
Default toggle between 0 and 1

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" wrote in message
...
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)


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 806
Default toggle between 0 and 1

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
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default toggle between 0 and 1

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" wrote in message
...
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" wrote in message
...
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)



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Toggle Between 2 Windows Aria[_2_] Excel Programming 1 May 15th 06 08:19 PM
Toggle Subtotal on and off Paul D. Simon Excel Programming 2 April 26th 06 11:38 AM
Toggle Acrobat add-in on/off R Avery Excel Programming 2 February 6th 04 11:26 PM
How to toggle Cesar Zapata[_2_] Excel Programming 3 November 15th 03 01:13 AM
toggle grid Todd[_5_] Excel Programming 4 August 31st 03 09:23 AM


All times are GMT +1. The time now is 12:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"