Re: Strange macro behavior - result is correct every other time

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On May 19, 12:54 am, "Tim Williams" <timjwilli...@xxxxxxxxxxx> wrote:
Maybe a better example might help.  The code you posted wouldn't give an
"answer": did you mean "exit for" and not "exit sub" ?

Since the code you have is too complex to explain, can you reduce it to a
smaller version which has the same problem ?  Without that it's unlikely
anyone could guess the cause of your problem.

You specifically mention that you're not using static variables, but you
*are* using global variables, which wouldn't lose their values without
resetting the environment with "Stop" or some other method.  Why don't you
suspect your globals ?

Tim

"Andrew" <andrewkgent...@xxxxxxxxx> wrote in message

news:0468a317-1cab-4d67-b3a0-b23baa3a771c@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Hello,
I am stumped.  I have a large VBA program with about 8 subroutines and
maybe 50-70 variables.  The program itself is too complicated to
explain, however as a gross approximation, it works about like this:

Private Sub Main_Clicl()
for x = 1 to 1000
if x^2+10x+25 = 100 then
exit sub
next
cells(1,1)=x
End Sub

So, the program would search for an answer.  Anyway, let's just say
that for this example, when I click on a button on the screen, I get
an answer in cells(1,1).  So, I click it one time and I get the
correct answer; I click a second time and I get an incorrect answer.
I click again, and I get the right answer.  And this pattern
continues.

I am not using any static variables.  All my variables are global.
Can someone please explain what might be happening.

thanks

Guys,
I wish I could submit the code, but I have 8 subroutines and several
hundred lines of code. I have run some output tests to verify that my
variables are all 0 prior to the function calls. I don't think there
is an easy answer here. I will have to dig line by line to find the
culprit.

.



Relevant Pages

  • Strange macro behavior - result is correct every other time
    ... I have a large VBA program with about 8 subroutines and ... Private Sub Main_Clicl ... I click a second time and I get an incorrect answer. ...
    (microsoft.public.excel.programming)
  • Re: Please help me grasp program structure
    ... > I'm making a Word VBA program to parse a table and make a new sheet ... > individual macros (subroutines) and call them from a main program? ... Jonathan West - Word MVP ... Please reply to the newsgroup -- ...
    (microsoft.public.word.vba.beginners)
  • Re: Disadvantages of "our" v. "my" when declaring variables
    ... Subroutines accept arguments for a reason. ... Global variables are accessible by the ... mistake that causes a bug that makes the code do the wrong thing. ... should probably avoid global /state/ and try to limit scope as much as ...
    (perl.beginners)
  • Please help me grasp program structure
    ... I'm making a Word VBA program to parse a table and make a new sheet ... individual macros (subroutines) and call them from a main program? ...
    (microsoft.public.word.vba.beginners)
  • Re: Disadvantages of "our" v. "my" when declaring variables
    ... OC> I've started experimenting with the use of subroutines in place of ... repeating blocks of code in a few of my programs. ... OC> global variables using "our." ...
    (perl.beginners)