+ Reply to Thread
Results 1 to 4 of 4

Loop runs over its last count in a code with error handling? (With example)

  1. #1
    Forum Contributor
    Join Date
    08-29-2017
    Location
    London England
    MS-Off Ver
    2016
    Posts
    194

    Loop runs over its last count in a code with error handling? (With example)

    Hi all,

    Example below.

    My problem is; that the code works like a charm, no run-time errors or any of the sorts, there is error handling in my code with user-forms (with simple input-boxes it was confusing) they work great too, except in the last row instead of my loop ending, it runs over to the next row and if I input something in the user-forms then it will be a new row and then carries on to the next row, and becomes an endless loop. I can only exit the loop if I hit no for the msgbox, then it starts my next loop like an angel, does the job again, no issues, but again tries to become an endless loop in the last row.

    See the example for more info!

    I don't know why this happens.. or how to fix it.. Could someone help me with this?

    (Sorry for the lots of repetition in my code I am a new to VBA and its easier for me to read it this way)
    Attached Files Attached Files

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,919

    Re: Loop runs over its last count in a code with error handling? (With example)

    Wow! First of all, congratulations on using Excel Tables. You've certainly done your homework on how to address the parts of the table.

    Your assessment on duplicating code is accurate. This is not necessarily a bad thing but it does make it more difficult to maintain. You may have to go to multiple parts of the code to fix a bug or change the code. You do have the potential to clean this up since you appear to be using a very consistent naming convention for regions and table names throughout.

    I might be able to reverse engineer the code and figure out what is going on, but I believe that there is an entirely different way of doing this.

    First, I'd like to know what the relationship between Table1 and Table2 is. I can see that they are using two runs to create the data.

    We could use the information in Info&Admin and VLOOKUPS in Table1 and Table2 to pull the region in as "helper columns". Then we can run pivot tables against the table and loop through the filters by region. This will produce results by region one at a time. We can overlay the results of the pivot table with a named dynamic range and copy / paste that into the appropriate report section. It will still take programming, but Excel will be doing the bulk of the work and VBA would only be needed to sew things together.

    Let me know if you are interested in this approach. In the process I'll show you a couple of "pointers" (at least that's what I call them).

    Also, if it would not compromise anything, could you use real headers in Table1 and Table2? The rest of the data is sanitized enough.

    I'm always willing to help out a fellow GI (USAF Retired).
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Contributor
    Join Date
    08-29-2017
    Location
    London England
    MS-Off Ver
    2016
    Posts
    194

    Re: Loop runs over its last count in a code with error handling? (With example)

    Thank you very much for that, I certainly would be interested in coaching, but I had a deadline for this project, which is also the reason why I haven't replied to this.

    I managed to solve it today.. the Exit Sub was in the wrong place.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,278

    Re: Loop runs over its last count in a code with error handling? (With example)

    Rule 03: Cross-posting Without Telling Us

    Your post does not comply with Rule 3 of our Forum RULES. Do not cross-post your question on multiple forums without telling us about your threads on other forums.

    Post a link to any other forums where you have asked the same question. If you have fewer than 10 posts here, you will not be able to post a link, but you must still tell us where else you have asked the question.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    You have been a member long enough to know about this requirement. Please read our rules before posting here again.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ 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. Error handling loop vba
    By JSmith1504 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-21-2015, 07:26 AM
  2. [SOLVED] Error Handling in a loop
    By Turtleman10 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2013, 10:27 AM
  3. [SOLVED] Using Error handling within a Loop
    By uniquelydeep in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2013, 03:41 PM
  4. Error Handling in a Loop
    By amdk8800 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-28-2013, 03:46 PM
  5. Error handling within a Do Loop
    By Boondoggler in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2012, 12:15 AM
  6. Handling Pivot Tables with macros - code runs slow
    By Pichingualas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-12-2012, 10:58 AM
  7. Error handling within loop
    By DesignMind in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2009, 01:51 PM

Tags for this Thread

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