+ Reply to Thread
Results 1 to 6 of 6

Macro skipping If statements when run but works in debug

  1. #1
    Registered User
    Join Date
    01-12-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    4

    Macro skipping If statements when run but works in debug

    I have a sub that when I run it in debug mode and run it step by step it runs perfectly, however when you let it run by itself it skips half of the if statements. Here is the code:

    Please Login or Register  to view this content.
    When I run this step by step the debug.print printed 1113 (inserted 4 “header rows” to the top of the pages) but when I run it only prints 13 (inserted 2 “header rows”). So it looks like it got the first one for Positive but not the next two. At first I thought it had to do with the data that is in the cell that it is looking at, but given the same data run and debug return different results. I have tried to add sleep time to this to see if the link was the time I was added when debugging but that did not work. Any insight or suggestions would be greatly appreciated.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro skipping If statements when run but works in debug

    Really weird! My poor guess would:
    Your code has lots of activate and select. I suspect when you run it byitself, you are probably skipping these steps

  3. #3
    Registered User
    Join Date
    01-12-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro skipping If statements when run but works in debug

    Yeah I see what you are saying about a lot of activates and selects. If you think it would help I can try to add the header rows that I am inserting into the same sheet, just outside the print area.That would get rid of most of the activates. Its at least somewhere to start. However, I understand that it might be skipping those activates but would that also explain why it did not print the output the same?

    Thanks for the help, I will let you know if removing the activates helped.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro skipping If statements when run but works in debug

    I think this line is the culprit

    Please Login or Register  to view this content.
    If your code are unable to find pb, it skips to the end of the macro. The way your code is structured is if the if for ech pb is false, the code skips all steps and goes down straight to the end. So, you need to see if you have pb in your books

  5. #5
    Registered User
    Join Date
    01-12-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro skipping If statements when run but works in debug

    But that really doesn't explain why with the exact same data the debug finds all of the page breaks and the run finds 1/2 (for the example I provided) of the page breaks. And I know the sheet has page breaks in it because the code explicitly tells it to switch to that view, and you can see that they are there when it runs.
    Please Login or Register  to view this content.
    Or, am I missing something about what you are trying to explain?

    Update:
    To your note about the For Each loop. That is correct if there are no page breaks then it skips everything and just uses the section headers that are already there. Verified this was the case and it worked correctly.

    Tried the removing all of the Activate sheet byt placing the rows I want as a header in the same sheet, with no luck. I also tried making all of the ElseIf statements to If statements, since it looked like it was skipping a few of the ElseIf Statements, that to did not help either.
    Last edited by peage1475; 01-12-2013 at 06:52 PM.

  6. #6
    Registered User
    Join Date
    01-12-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro skipping If statements when run but works in debug

    I replaced the
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    and everything worked great.

+ 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