+ Reply to Thread
Results 1 to 5 of 5

IF statement in .txt file import

  1. #1
    Registered User
    Join Date
    04-20-2012
    Location
    New York, NY
    MS-Off Ver
    Excel 2013, 2010, 2007, 2003
    Posts
    99

    IF statement in .txt file import

    Hello friends,

    I use the following code to import all of the .txt files in a directory into the "Import" sheet of my workbook.

    Please Login or Register  to view this content.

    I run this process twice, using two different workbooks to import two different file types (both .txt, just containing different data sets) and I recently decided I would like to try and combine them into one operation. What I'd like to do is add an IF condition to my Import code such as:

    IF the file name contains "confirm" THEN import to Sheets("Confirm") ELSE import to Sheets("Import")

    For example, if my directory contains the following files:

    7515F01_120511_05-10_191510.txt
    7515F01_120512_05-10_191510.txt
    7515F01_120513_05-10_191510.txt
    7515F01_120511_confirm_05-10_191510.txt
    7515F01_120512_confirm_05-10_191510.txt
    7515F01_120512_confirm_05-10_191510.txt

    The top three would go to the "Import" sheet and the bottom three would go to the "Confirm" sheet.

    My VBA skills are, how do you say... developing, so any guidance is greatly appreciated.

    Thank you.
    SPARTAN
    Please click the * if my solution helped

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: IF statement in .txt file import

    Hello singerj6,

    Try this revision to the macro...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: IF statement in .txt file import

    Does everyone knows Excel as well as you do in San Francisco Leith?

  4. #4
    Registered User
    Join Date
    04-20-2012
    Location
    New York, NY
    MS-Off Ver
    Excel 2013, 2010, 2007, 2003
    Posts
    99

    Re: IF statement in .txt file import

    Hi Leith, thank you for the speedy reply.

    When I run this code I get a "Compile error: Sub or Function not defined" error with "Createaobject" highlighted on the Set fso = Createaobject("Script.FileSystemObject") line

    Thanks again

  5. #5
    Registered User
    Join Date
    04-20-2012
    Location
    New York, NY
    MS-Off Ver
    Excel 2013, 2010, 2007, 2003
    Posts
    99

    Re: IF statement in .txt file import

    Update: Was able to combine our two sets of code to come up with this, which works beautifully. Thanks for your assistance!

    Please Login or Register  to view this content.

+ 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