Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a second equals = within vba variable assignment do?

Perplexed by the function of using a second = sign in vba. eg. s = Int(xVal) + (xVal = n + 1)

I had been deciphering some code and came across the following line which is perplexing me somewhat and despite some extensive research and debugging I seem to be struggling to find the answer:

s = Int(xVal) + (xVal = n + 1)

and

p(i, 3) = A(i)(s + 3 + (s = n)) + (s = n) * (p(i, 1) - p(i, 2))

My question is what is the function of the comparisons within the parentheses after the first assignment = sign?

TIA

like image 863
ShaunMc Avatar asked Jul 05 '26 14:07

ShaunMc


1 Answers

(s = n)

If both s and n have the same value then this evaluates to True, which can be coerced to its underlying value of -1 by other arithmetic operations.

Eg:

? True * 1   '>> -1
? False * 1   '>> 0

So this:

s = Int(xVal) + (xVal = n + 1)

is like writing:

If xVal = n + 1 Then
    s = Int(xVal) + -1
else
    s = Int(xVal) + 0
end if

or:

s = Int(xVal) + IIf(xVal = n + 1, -1, 0)
like image 145
Tim Williams Avatar answered Jul 07 '26 07:07

Tim Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!