very basic question. Does anyone know if there is a VBA-equivalent
of the "+=" operator in C.
i+=1 instead of i=i+1
No, there is not such an equivalent. However, you can sort-of construct one
if you want to. Although requiring more typing than in C/C++, I thought you
might find this old (originally compiled VB, but works for VBA) post of mine
somewhat enjoyable...
Rick
In the past, I have posted a consolidated approach to prefix and postfix
operator functions. Here are three related posts that dealt with this
issue... I thought you might find them interesting.
POSTING #1
========================================
What about if we construct an equivalent to the ++ operator (A -- function
is just as easy to construct). Consider this function
Function PP(Arg1 As Variant, Arg2 As Variant, _
Optional Increment As Long = 1) As Variant
Dim ErrorCondition As Long
If Increment < 0 Then
ErrorCondition = 380 'Invalid property value -- must be positive
ElseIf Arg1 = "++" Then 'Prefix
If VarType(Arg2) > 3 Then
ErrorCondition = 13 'Type mismatch - not Integer or Long
Else
Arg2 = Arg2 + Increment
PP = Arg2
End If
ElseIf Arg2 = "++" Then 'Postfix
If VarType(Arg1) > 3 Then
ErrorCondition = 13 'Type mismatch - not Integer or Long
Else
PP = Arg1
Arg1 = Arg1 + Increment
End If
Else
ErrorCondition = 93 'Invalid Pattern String -- no "++" specified
End If
If ErrorCondition Then Err.Raise ErrorCondition
End Function
where PP is meant to stand for the ++ (plus plus) operator. The
prefix/postfix operation is controlled by placing the string "++" either in
the first or second argument (and your Integer or Long variable in the other
of those two arguments). If the first argument is set as "++", then a prefix
operation is performed. If the second argument is set as "++", then a
postfix operation is performed.
So, the C/C++ loop
while(n++ < 20)
{
.....
}
would become
Do While PP(n, "++") < 20
...
Loop
To duplicate this C/C++ loop instead
while(++n < 20)
{
.....
}
you would use
Do While PP("++", n) < 20
...
Loop
Of course this is not as compact as the C/C++ notation, but it is fully
equivalent in function. You may use it in any calculation, not just in loop
control tests.
Oh, and one final note. The optional third argument allows you to set an
increment value other than 1. So if you wanted to use a number in a
calculation and then increment it by say 5 afterwards. Then after executing
this postfix operation
SomeValue = 3
NewValue = PP(SomeValue, "++", 5)
NewValue is assigned 3 and SomeValue becomes 8 after the last statement.
This is equivalent to
SomeValue = 3
NewValue = SomeValue
SomeValue = SomeValue + 5
We could follow a similar construction in order to create a MM
(minus-minus); and even a TT(times-times; actually multiply-multiply) or
DD(divide-divide).
POSTING #2
========================================
Actually, that last example in the "one final note" section might have been
more instructive if I used the result from PP, something like this:
SomeValue = 3
NewValue = 10 * PP(SomeValue, "++", 5)
NewValue is assigned 30 and SomeValue becomes 8 after the last statement.
This is equivalent to
SomeValue = 3
NewValue = 10 * SomeValue
SomeValue = SomeValue + 5
And, of course, this
SomeValue = 3
NewValue = 10 * PP("++", SomeValue, 5)
sets NewValue to 80 (the SomeValue variable is incremented *before* it is
used) and SomeValue to 8. This is equivalent to
SomeValue = 3
SomeValue = SomeValue + 5
NewValue = 10 * SomeValue
POSTING #3
========================================
Actually, most of the routine dealt with error detection. If you could trust
yourself to always input the correct parameters <g>, everything boils down
to this:
Function PP(Arg1 As Variant, Arg2 As Variant, _
Optional Increment As Long = 1) As Variant
If Arg1 = "++" Then 'Prefix
Arg2 = Arg2 + Increment
PP = Arg2
ElseIf Arg2 = "++" Then 'Postfix
PP = Arg1
Arg1 = Arg1 + Increment
End If
End Function
This code is really not that bad; you should find it easier to read.