+ Reply to Thread
Results 1 to 4 of 4

Prevent Do While loop from doing a Go to Error

  1. #1
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Prevent Do While loop from doing a Go to Error

    Hi,

    I've got the following code which opens/gets the excel files from a specified directory and imports them into an open workbook. I've added a GoTo Error before my loop which checks if there are any files in the directory and shows an error message if it's the case. Problem is since the loop goes on till there are no more files in the directory, it goes back to the error. Any ideas on how to change that ?

    Please Login or Register  to view this content.
    Thanks,
    amphi

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Prevent Do While loop from doing a Go to Error

    Put exit sub at the end of the loop, i.e.:
    Please Login or Register  to view this content.
    Edit:
    or
    Please Login or Register  to view this content.
    and get rid of the goto bit

  3. #3
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Prevent Do While loop from doing a Go to Error

    The line after Error: will run every time the loop is complete whether or not there is an error.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  4. #4
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Prevent Do While loop from doing a Go to Error

    Thanks ragulduy! I went with the second option as I still had some procedures to run after the loop.

+ 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. Nesting a loop within a loop error
    By PunPryde in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2011, 08:07 PM
  2. Prevent a Saving Error
    By JamesT1 in forum Excel General
    Replies: 7
    Last Post: 08-26-2008, 01:55 PM
  3. Prevent #VALUE error
    By Ducatisto in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-27-2008, 09:36 AM
  4. How to prevent Worksheet_Change loop?
    By HotRod in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-05-2005, 11:06 AM
  5. Prevent Error Message
    By phil in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-26-2005, 03:06 PM

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