+ Reply to Thread
Results 1 to 8 of 8

Move to next file if condition fails

  1. #1
    Registered User
    Join Date
    02-14-2017
    Location
    India
    MS-Off Ver
    2007
    Posts
    15

    Question Move to next file if condition fails

    Hello

    I'm running a macro to open files in a folder and process the data in excel
    Steps:
    1. Open file
    2. Look for a condition (Job ID Exists)
    a. PASS -> further processing, excel entries, close file, loop onto next file
    b. FAIL -> I would like the macro to close that particular file and move onto the next file in the folder

    I've attached the current macro that I have now
    where I am able to open the file and look for pass condition and process the file
    however, I am struggling with the fail condition


    Please Login or Register  to view this content.
    Please help

    Thanks
    P

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Move to next file if condition fails

    A little reorganizing overall, then a simple EXIT DO for your loop:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-14-2017
    Location
    India
    MS-Off Ver
    2007
    Posts
    15

    Re: Move to next file if condition fails

    Hello Jerry

    Thanks for your reply
    The code that you have sent works well

    However, it doesn't work when I add the rest of my code

    Let me add that here

    Please Login or Register  to view this content.

    Quote Originally Posted by JBeaucaire View Post
    A little reorganizing overall, then a simple EXIT DO for your loop:
    Please Login or Register  to view this content.
    Please help

    Thanks in advance
    P
    Last edited by pratnimk; 02-16-2017 at 10:22 AM.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Move to next file if condition fails

    OK, so when that first IF statement kicks in the first loop and the test is FALSE, what exactly is to supposed happen? There are two loops in there now, so exiting the first as I demonstrated would then close the first and proceed to the second. What is wrong? What should happen? Be specific, note code lines if necessary.

  5. #5
    Registered User
    Join Date
    02-14-2017
    Location
    India
    MS-Off Ver
    2007
    Posts
    15

    Re: Move to next file if condition fails

    Hello Jerry

    Apologies for being ambiguous in my previous post
    What I'm looking for is

    1. Open the file
    2. Check for condition -> text = JobID (1st loop)
    3. If JobID exists (Condition - Pass) move to next loop where it retrieves the report path and then the loop after (not added as it is written in the same way) to retrieve other text from the file
    I've written a separate loop for each of the other parts of text due to lack of knowledge on how to get it done together in one single loop
    (Yes I know it is performance in-efficient )
    4. If JobID does not exist - it should simply exit this loop and not move to the next loop either and move to next file itself

    I'm not sure if this can be achieved in a single loop...can it?

    Please advise

    Thanks
    P

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Move to next file if condition fails

    Not going to rewrite the whole macro, but you can use a GOTO command in the ELSE to jump to the just above the second CLOSE #1 and that will effectively skip all that code...

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-14-2017
    Location
    India
    MS-Off Ver
    2007
    Posts
    15

    Re: Move to next file if condition fails

    Hello Jerry

    This resolved my issue

    Thanks a lot for bearing with me

    Cheers
    Thanks
    P

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Move to next file if condition fails

    As it appears you've reached a conclusion, I've marked this thread SOLVED for you.
    FYI, this is done through the Thread Tools located above the first post in this thread. Thanks.

+ 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. [SOLVED] Loops are driving me loopy! How to exit a loop as soon as condition fails
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 03-22-2016, 12:39 AM
  2. Code Fails Saving File
    By breader in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-27-2013, 03:06 AM
  3. Highlight data with some color if condition fails
    By jesika in forum Access Tables & Databases
    Replies: 2
    Last Post: 04-08-2009, 07:35 AM
  4. File fails to calculate - corrupted?
    By bahguy in forum Excel General
    Replies: 3
    Last Post: 08-09-2007, 03:53 AM
  5. Hyperlink fails to open a PDF file
    By XP in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2006, 04:35 PM
  6. Replies: 2
    Last Post: 12-20-2005, 09:50 PM
  7. Excel fails to lock file across VPN?
    By LongJudson in forum Excel General
    Replies: 0
    Last Post: 02-07-2005, 01: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