Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Is there a way to write a function to increment a variable (i.e. to
replace "myvar = myvar + 1")? For example, I would like to create a custom function called "inc" so I can do: inc myvar This would require somehow passing a variable name to the function (which I don't see how is possible). Any other ideas how to get around the "myvar = myvar + 1" ? Thanks! |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
One way:
Public Function inc(ByRef vIn As Variant) If IsNumeric(vIn) Then vIn = vIn + 1 End Function In article . com, " wrote: Is there a way to write a function to increment a variable (i.e. to replace "myvar = myvar + 1")? For example, I would like to create a custom function called "inc" so I can do: inc myvar This would require somehow passing a variable name to the function (which I don't see how is possible). Any other ideas how to get around the "myvar = myvar + 1" ? Thanks! |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Maybe I'm misunderstanding something, but I wanted to create a function
that you could pass any variable to and have it increment that variable: inc myvar1 inc myvar2 inc myvar3 Wouldn't JE's example just increment a variable called "vln"? I also know it's a small performance hit, but for speed in coding, it's worth it to me. Thanks! |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
No.
When you call inc using inc myvar1 then myvar1 is passed by reference to inc, and will be assigned the value that is assigned to vIn. From XL/VBA Help ("by reference"): a way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by reference. In article .com, " wrote: Wouldn't JE's example just increment a variable called "vln"? |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I've been programming for 17 years and have misunderstood ByRef the
whole time. I thought it meant that you could modify the variable and keep the new value ONLY if you passed the same name into the procedure. Ex. myvar = 4 sub inc(byref myvar as integer) myvar = myvar + 1 end sub - myvar = 5 I didn't know you could call the sub variable anything you want in the procedure and the sub variable would still point back to the orignal variable. Since ByRef is default, I don't quite understand how I've called 1000's and 1000's of subs, passed variables, used them in calculations, and then continue the main sub without that variable causing problems. The only thing I can think of is that I must be assigning the variables right before I pass them so even though they're getting changed, I reassign the correct value before calling the sub again. Very interesting discovery in terms of never causing problems in my programming. Thanks for your BASIC insight... |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Increment/Increment letter in alphabetical order | Excel Programming | |||
Increment variable not incrementing? | Excel Programming | |||
Increment a Variable Defined Date by 1 day | Excel Programming | |||
Increment a variable | Excel Programming | |||
Increment a variable while looping | Excel Programming |