Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello
very basic question. Does anyone know if there is a VBA-equivalent of the "+=" operator in C. i+=1 instead of i=i+1 thanks Charles |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Aug 23, 3:21 am, Charles wrote:
Hello very basic question. Does anyone know if there is a VBA-equivalent of the "+=" operator in C. i+=1 instead of i=i+1 thanks Charles AFAIK there is none. Alok |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 ======================================== Hey Rick, thats pretty good. I just want to say, don't you hate the amount of code that VB requires as oposed to C++. I wish they could some day reduce this, its just to word'y... 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. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I just realized that you did not ask the question I responded to; however, I
hope you found the Prefix/Postfix code interesting nonetheless. And to answer your original question... no, there is no short cut "assignment incrementer" equivalent to the C/C++ version of i+=1. Rick "Rick Rothstein (MVP - VB)" wrote in message ... 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 ======================================== Hey Rick, thats pretty good. I just want to say, don't you hate the amount of code that VB requires as oposed to C++. I wish they could some day reduce this, its just to word'y... 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. |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
That basically answers my question. I must say I have never been a
great fan of C/C++ (too rigorous for me) but I regret a few functions like that one... thanks! Charles |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Does anyone know if there is a VBA-equivalent
of the "+=" operator in C. i+=1 instead of i=i+1 Here' a workaround that I use for the Increment function. I also have a "Decrement" function as well. Function Inc(ByRef n) '// Increment by +1 n = n + 1 End Function Sub Demo() Dim AVeryLongVariableName AVeryLongVariableName = 5 AVeryLongVariableName = AVeryLongVariableName + 1 'or Inc AVeryLongVariableName End Sub HTH Dana DeLouis Charles wrote: That basically answers my question. I must say I have never been a great fan of C/C++ (too rigorous for me) but I regret a few functions like that one... thanks! Charles |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
i+=1 instead of i=i+1
Just to mention if you like it. When we just want to increase a variable by 1, we most likely would use the PreIncrement function "++" (ie ++ v) In vba, it's generally not recommended, but we can have fun with function names in a few cases. I also have the Inc function, but whose name is ++, or the characters Alt + 0134 (twice) Hold down the Alt key and type 0134. Function ††(ByRef n) ' Alt + 0134 n = n + 1 End Function Sub Demo() Dim n n = 5 †† n End Sub 'n is now 6. You have to use a little imagination to find characters that are close to what you need. In addition, not all characters are valid. HTH Dana DeLouis Dana DeLouis wrote: Does anyone know if there is a VBA-equivalent of the "+=" operator in C. i+=1 instead of i=i+1 Here' a workaround that I use for the Increment function. I also have a "Decrement" function as well. Function Inc(ByRef n) '// Increment by +1 n = n + 1 End Function Sub Demo() Dim AVeryLongVariableName AVeryLongVariableName = 5 AVeryLongVariableName = AVeryLongVariableName + 1 'or Inc AVeryLongVariableName End Sub HTH Dana DeLouis Charles wrote: That basically answers my question. I must say I have never been a great fan of C/C++ (too rigorous for me) but I regret a few functions like that one... thanks! Charles |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Text "comparison" operator for "contains" used in an "IF" Function | Excel Worksheet Functions | |||
Is it possible to apply IF's to "operator" result.. | Excel Discussion (Misc queries) | |||
program stuck at "Save Cancelled by Operator" | Excel Discussion (Misc queries) | |||
How 2 use right shift "<<" and left shift "" operator in excel? | Excel Programming | |||
Excel no longer allows new formulas with division "/" operator | Excel Programming |