+ Reply to Thread
Results 1 to 5 of 5

strange behaviour - no feedback!

  1. #1

    strange behaviour - no feedback!

    Hi,
    I`ve done some rather basic programming in excel and in general I find
    vba a useful tool. However, from time to time I run into some problems
    that are very annoying because the program is unable to report on what
    line of code causes problems. Today, I tried to run a macro that enters
    a loop like

    for counter = 0 to pi_maxnum

    but the code never entered the loop and the program just stopped and
    I`m right back in manual control over excel. The solution was to
    hardcode the loop like

    for counter = 0 to 10

    and everything ran fine.

    The pi_maxnum was defined as public in another module, I also tried
    defining it as public in the module with the sub containing the loop,
    to no effect.

    I`m no vba expert, someone here can probably point out the problem, but
    when the system never even to reports on why nothing happens .. it
    should be a law against it. In this case I would excpect a message on
    something wrong with the definition of pi_maxnum.

    Anyway, if someone cares to share some thoughts on this I`d be glad.


  2. #2
    Norman Jones
    Guest

    Re: strange behaviour - no feedback!

    Hi Epost,

    My guess would be that the variable is not receiving/retaining a value. If
    pi_maxnum is not initialised, and therefore has a value of zero, the loop
    would run from 0 to 0 (i.e. not execute) and there would be no error to
    report.

    Check therefore that the public variable is being correctly initialised and
    that you are *not* declaring the variable (again) in a sub. Were this the
    case, the value of the variable in the sub would be that used and, if it is
    not initialised in the sub, it would have a zero value.


    ---
    Regards,
    Norman



    <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    > I`ve done some rather basic programming in excel and in general I find
    > vba a useful tool. However, from time to time I run into some problems
    > that are very annoying because the program is unable to report on what
    > line of code causes problems. Today, I tried to run a macro that enters
    > a loop like
    >
    > for counter = 0 to pi_maxnum
    >
    > but the code never entered the loop and the program just stopped and
    > I`m right back in manual control over excel. The solution was to
    > hardcode the loop like
    >
    > for counter = 0 to 10
    >
    > and everything ran fine.
    >
    > The pi_maxnum was defined as public in another module, I also tried
    > defining it as public in the module with the sub containing the loop,
    > to no effect.
    >
    > I`m no vba expert, someone here can probably point out the problem, but
    > when the system never even to reports on why nothing happens .. it
    > should be a law against it. In this case I would excpect a message on
    > something wrong with the definition of pi_maxnum.
    >
    > Anyway, if someone cares to share some thoughts on this I`d be glad.
    >




  3. #3
    Tom Ogilvy
    Guest

    Re: strange behaviour - no feedback!

    In the same module, run this as a test. Make the immediate window is
    visible.

    Sub aa()
    For counter = 0 To pi_maximum
    Debug.Print counter
    Next
    End Sub

    see if the loop is executed

    the problem you are dealing with is either a logic error or a failure on
    your part to understand variable scope. It isn't something VBA/Excel would
    consider an error - because people might intentionally design their code so
    the loop is never executed under certain conditions. By using an variable as
    one of the loop conditions, you are taking responsibility for determining if
    the loop will run or not. So if you want a warning, you will have to
    include that in the logic of your code.

    --
    Regards,
    Tom Ogilvy

    -




    <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    > I`ve done some rather basic programming in excel and in general I find
    > vba a useful tool. However, from time to time I run into some problems
    > that are very annoying because the program is unable to report on what
    > line of code causes problems. Today, I tried to run a macro that enters
    > a loop like
    >
    > for counter = 0 to pi_maxnum
    >
    > but the code never entered the loop and the program just stopped and
    > I`m right back in manual control over excel. The solution was to
    > hardcode the loop like
    >
    > for counter = 0 to 10
    >
    > and everything ran fine.
    >
    > The pi_maxnum was defined as public in another module, I also tried
    > defining it as public in the module with the sub containing the loop,
    > to no effect.
    >
    > I`m no vba expert, someone here can probably point out the problem, but
    > when the system never even to reports on why nothing happens .. it
    > should be a law against it. In this case I would excpect a message on
    > something wrong with the definition of pi_maxnum.
    >
    > Anyway, if someone cares to share some thoughts on this I`d be glad.
    >




  4. #4

    Re: strange behaviour - no feedback!

    Ok, thanks a lot for your help, guess I need to put in some tests in my
    code. It was not a problem with the variable pi_maxnum, it was declared
    and given a value. In this case the problem was a logical error on my
    part, actually the statement was

    for counter = 42 to (counter + pi_maxnum)

    I know, its nonsene, and I appologize for asking the wrong question. I
    was somehow convinced I gave a correct minimal example (believe it or
    not).

    This loop executes only if I let counter run from a value smaller than
    or equal to pi_maxnum. pi_maxnum was 36 in this case. If I say

    for counter = 36 to (counter + pi_maxnum)

    loop executes. But with

    for counter = 37 to (counter + pi_maxnum)

    nothing happens. I guess (counter + pi_maxnum) becomes equal to
    pi_maxnum, and that counter is not evaluated on the right hand side.
    Seems to me that ideally, as long as pi_maxnum > 0, this should be an
    endless loop regardless of the inital value of counter.


    Tom Ogilvy skrev:
    > In the same module, run this as a test. Make the immediate window is
    > visible.
    >
    > Sub aa()
    > For counter = 0 To pi_maximum
    > Debug.Print counter
    > Next
    > End Sub
    >
    > see if the loop is executed
    >
    > the problem you are dealing with is either a logic error or a failure on
    > your part to understand variable scope. It isn't something VBA/Excel would
    > consider an error - because people might intentionally design their code so
    > the loop is never executed under certain conditions. By using an variable as
    > one of the loop conditions, you are taking responsibility for determining if
    > the loop will run or not. So if you want a warning, you will have to
    > include that in the logic of your code.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > -
    >
    >
    >
    >
    > <[email protected]> wrote in message
    > news:[email protected]...
    > > Hi,
    > > I`ve done some rather basic programming in excel and in general I find
    > > vba a useful tool. However, from time to time I run into some problems
    > > that are very annoying because the program is unable to report on what
    > > line of code causes problems. Today, I tried to run a macro that enters
    > > a loop like
    > >
    > > for counter = 0 to pi_maxnum
    > >
    > > but the code never entered the loop and the program just stopped and
    > > I`m right back in manual control over excel. The solution was to
    > > hardcode the loop like
    > >
    > > for counter = 0 to 10
    > >
    > > and everything ran fine.
    > >
    > > The pi_maxnum was defined as public in another module, I also tried
    > > defining it as public in the module with the sub containing the loop,
    > > to no effect.
    > >
    > > I`m no vba expert, someone here can probably point out the problem, but
    > > when the system never even to reports on why nothing happens .. it
    > > should be a law against it. In this case I would excpect a message on
    > > something wrong with the definition of pi_maxnum.
    > >
    > > Anyway, if someone cares to share some thoughts on this I`d be glad.
    > >



  5. #5
    Tom Ogilvy
    Guest

    Re: strange behaviour - no feedback!

    the loop boundaries are calculated upon reaching the For statement the first
    time and never recalculate. I would assume that on entry, counter is not
    initialized, so your upper bound was equivalent to pi_maximum alone.

    --
    Regards,
    Tom Ogilvy


    <[email protected]> wrote in message
    news:[email protected]...
    > Ok, thanks a lot for your help, guess I need to put in some tests in my
    > code. It was not a problem with the variable pi_maxnum, it was declared
    > and given a value. In this case the problem was a logical error on my
    > part, actually the statement was
    >
    > for counter = 42 to (counter + pi_maxnum)
    >
    > I know, its nonsene, and I appologize for asking the wrong question. I
    > was somehow convinced I gave a correct minimal example (believe it or
    > not).
    >
    > This loop executes only if I let counter run from a value smaller than
    > or equal to pi_maxnum. pi_maxnum was 36 in this case. If I say
    >
    > for counter = 36 to (counter + pi_maxnum)
    >
    > loop executes. But with
    >
    > for counter = 37 to (counter + pi_maxnum)
    >
    > nothing happens. I guess (counter + pi_maxnum) becomes equal to
    > pi_maxnum, and that counter is not evaluated on the right hand side.
    > Seems to me that ideally, as long as pi_maxnum > 0, this should be an
    > endless loop regardless of the inital value of counter.
    >
    >
    > Tom Ogilvy skrev:
    > > In the same module, run this as a test. Make the immediate window is
    > > visible.
    > >
    > > Sub aa()
    > > For counter = 0 To pi_maximum
    > > Debug.Print counter
    > > Next
    > > End Sub
    > >
    > > see if the loop is executed
    > >
    > > the problem you are dealing with is either a logic error or a failure on
    > > your part to understand variable scope. It isn't something VBA/Excel

    would
    > > consider an error - because people might intentionally design their code

    so
    > > the loop is never executed under certain conditions. By using an

    variable as
    > > one of the loop conditions, you are taking responsibility for

    determining if
    > > the loop will run or not. So if you want a warning, you will have to
    > > include that in the logic of your code.
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > > -
    > >
    > >
    > >
    > >
    > > <[email protected]> wrote in message
    > > news:[email protected]...
    > > > Hi,
    > > > I`ve done some rather basic programming in excel and in general I find
    > > > vba a useful tool. However, from time to time I run into some problems
    > > > that are very annoying because the program is unable to report on what
    > > > line of code causes problems. Today, I tried to run a macro that

    enters
    > > > a loop like
    > > >
    > > > for counter = 0 to pi_maxnum
    > > >
    > > > but the code never entered the loop and the program just stopped and
    > > > I`m right back in manual control over excel. The solution was to
    > > > hardcode the loop like
    > > >
    > > > for counter = 0 to 10
    > > >
    > > > and everything ran fine.
    > > >
    > > > The pi_maxnum was defined as public in another module, I also tried
    > > > defining it as public in the module with the sub containing the loop,
    > > > to no effect.
    > > >
    > > > I`m no vba expert, someone here can probably point out the problem,

    but
    > > > when the system never even to reports on why nothing happens .. it
    > > > should be a law against it. In this case I would excpect a message on
    > > > something wrong with the definition of pi_maxnum.
    > > >
    > > > Anyway, if someone cares to share some thoughts on this I`d be glad.
    > > >

    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1