+ Reply to Thread
Results 1 to 15 of 15

Final lines of code are ignored - can't understand why

  1. #1
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Final lines of code are ignored - can't understand why

    I am using the following code to insert blank rows. By starting at the bottom of column H and stepping upward one cell at a time, i compare that cell to the one below it. If they are not identical, I insert a blank row between them. This is the code:

    Please Login or Register  to view this content.
    This executes without a problem, working as it is intended. However there is a small portion of code after it that does not seem to execute. It looks at the blank rows and fills them with no-fill in case they may contain color from previous instructions. When I run this as a stand-alone routine, it works as intended. But it will not run in the sub routine that contains the code above that inserts blank rows. I have never used the "GoTo" command, and I am wondering if it is causing problems.

    Please Login or Register  to view this content.
    Last edited by Wedge120; 01-23-2015 at 01:38 PM.

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Final lines of code are ignored - can't understand why

    "IF" has two formats:

    1.)
    If x = y Then
    'do stuff
    End If

    2.)
    If x = y Then 'do stuff


    If you include actions on the same line proceeding the Then, everything else is ignored.

    Please Login or Register  to view this content.
    I'm not sure how this isn't throwing ElseIf without If compiling error.
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Final lines of code are ignored - can't understand why

    Try changing the first part too:

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: Final lines of code are ignored - can't understand why

    My mistake for not including a piece of code that was important. I removed an instruction that I thought would be confusing. It was a way to exit the subroutine when the iteration upward reached the header. Complete code for inserting blank rows below:

    Please Login or Register  to view this content.
    Last edited by Wedge120; 01-22-2015 at 06:50 PM.

  5. #5
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Final lines of code are ignored - can't understand why

    You should place all of your tags at the very end of your script, and manually add an Exit Sub before them.

    Your script never gets past GetNext because it is outside of the IF. There is no circumstance when it doesn't hit it.

    Examine this logic as an alternative:

    Please Login or Register  to view this content.
    Now my code can end before it gets to GetNext. In your code, even when GetNext doesn't apply it always runs into it.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Final lines of code are ignored - can't understand why

    Can you attach a sample file, showing us a before and after (how it should be) affect of the macro?

  7. #7
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: Final lines of code are ignored - can't understand why

    The difference is near the top, row 2. In before, it is yellow; in after, it's not filled.

    Wedge120-before.xls
    Wedge120-after.xls


    Here is the code as I currently have it written:

    Please Login or Register  to view this content.
    Last edited by Wedge120; 01-22-2015 at 06:44 PM.

  8. #8
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: Final lines of code are ignored - can't understand why

    Quote Originally Posted by daffodil11 View Post
    You should place all of your tags at the very end of your script, and manually add an Exit Sub before them.

    Your script never gets past GetNext because it is outside of the IF. There is no circumstance when it doesn't hit it.

    Examine this logic as an alternative:

    Please Login or Register  to view this content.
    Now my code can end before it gets to GetNext. In your code, even when GetNext doesn't apply it always runs into it.

    Is the "GetNext" a tag? I'm not familiar with tags. This is code written by another forum member that was helping me out a few weeks ago. At that time, however, I did not know (or inform him) that I would be writing more code after the tag.

  9. #9
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: Final lines of code are ignored - can't understand why

    Here a 2-birds-with-one stone approach. It places the tag at the end and gets rid of excess code. Still, if there are better ways, do share:

    Please Login or Register  to view this content.
    Last edited by Wedge120; 01-22-2015 at 07:08 PM.

  10. #10
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Final lines of code are ignored - can't understand why

    Can you attach a sample worksheet that this applies to, with some dummy data?
    Last edited by daffodil11; 01-22-2015 at 07:14 PM.

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Final lines of code are ignored - can't understand why

    From post #4, I would re-write this. Judicious use of GoTo acceptable, but too often it is overused leading to confusion.

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  12. #12
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: Final lines of code are ignored - can't understand why

    I've posted a "before" and "after" 5 posts up.

  13. #13
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: Final lines of code are ignored - can't understand why

    Quote Originally Posted by mikerickson View Post
    From post #4, I would re-write this. Judicious use of GoTo acceptable, but too often it is overused leading to confusion.

    Please Login or Register  to view this content.
    I like this, and if I were a smarter guy than I actually am, I might have noticed it could be written that way.

    Is there EVER a time that "goto" should really be used?

  14. #14
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Final lines of code are ignored - can't understand why

    Quote Originally Posted by Wedge120 View Post
    Is there EVER a time that "goto" should really be used?
    The "best practices" police will scream every time that they see a GoTo.
    There is always a work-around so one can avoid using it. (Often involving introducing a boolean variable)*

    The situation that I see where GoTo is appropriate is a looping AND.
    Consider the problem. "If every worksheet in a book has 1 in A1, add a new worksheet to the book, otherwise do nothing"
    One way to code that would be
    Please Login or Register  to view this content.
    More important (IMO) than the lexical rule "don't use GOTO" is the conceptual rule "One Way In, One Way Out"
    A procedure should only have one start point and all the branching should combine at the end so the End Sub line is actually excited. (Except for error conditions).
    They both lead away from "spaghetti code" where you can't tell what is going on next.

    *- This is a non-goto code that solves the above problem
    Please Login or Register  to view this content.
    I think the first code is easier to understand (especially in cases where the condition is more complicated).

    The guide should be, will you understand your code in six months when you revisit it to change something? Will some stranger whose never seen it before be able to decipher and alter it to their wishes?
    Last edited by mikerickson; 01-22-2015 at 10:18 PM.

  15. #15
    Forum Contributor
    Join Date
    11-05-2006
    Posts
    123

    Re: Final lines of code are ignored - can't understand why

    Marking as Solved. Thanks to you guys.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Please help me understand this lines
    By mikoski in forum Excel General
    Replies: 4
    Last Post: 07-13-2014, 11:56 PM
  2. [SOLVED] Understand what the following code does
    By Stanley91 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-08-2014, 06:14 AM
  3. Lines in final PDF printed document
    By cabinetguy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-21-2012, 10:36 AM
  4. Don't Understand Code
    By miracleblake in forum Excel General
    Replies: 3
    Last Post: 12-08-2005, 04:15 PM
  5. Help me understand this code
    By Carrot in forum Excel General
    Replies: 2
    Last Post: 10-14-2005, 01:07 AM

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