PDA

View Full Version : [SOLVED:] VBA question on counters



JLooch
01-30-2017, 08:34 AM
I used to program lots in old basic, I'm new with Excel VBA,
i have several loop counters and other placeholders in my main sub which I also use in some functions and subroutines etc

when these functions/subs get called upon, my placeholders or loop counters get reset all the time,
how do you keep the loop counters etc to remember/keep there "main sub" values, is there a way?
J

JKwan
01-30-2017, 08:39 AM
Yes, everytime you invoke the function/subs, they all get reset. One way to overcome this is to create a module variable or use the STATIC to dim your variable



dim GlobalVariable as long

sub blah
Static StaticVariable
GlobalVariable = .....
end sub

Aflatoon
01-30-2017, 08:47 AM
You should declare the variables locally to each routine so that calling another routine does not affect the value of the variable in the main routine.

JLooch
01-30-2017, 08:58 AM
Thanks a bunch guys, will try these out,