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)
|