Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Increment a variable?

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Increment a variable?

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Increment a variable?

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Increment a variable?

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Increment a variable?

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Increment/Increment letter in alphabetical order Neil Goldwasser Excel Programming 3 January 25th 06 09:07 AM
Increment variable not incrementing? [email protected] Excel Programming 3 November 19th 04 12:10 AM
Increment a Variable Defined Date by 1 day JimI Excel Programming 2 October 12th 04 12:57 AM
Increment a variable Harry[_3_] Excel Programming 0 April 9th 04 04:06 PM
Increment a variable while looping ibeetb Excel Programming 2 September 25th 03 07:15 PM


All times are GMT +1. The time now is 11:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"