+ Reply to Thread
Results 1 to 15 of 15

open all files in a folder with fixed number of characters in filename

  1. #1
    Registered User
    Join Date
    01-14-2012
    Location
    Darlington, England
    MS-Off Ver
    Excel 2003
    Posts
    9

    open all files in a folder with fixed number of characters in filename

    Hi all, I am fairly new to VBA but keen to learn.

    I want to open all the files in a folder whose filename consists of a certain number of characters. The filename will be either 6 or 8 characters long. I have two macros and would like one of them to open all the 6 character filenames and ignore the 8 character ones, while the second macro should only open 8 character filenames. I have found through the forum how to open all files of a given type eg "book1*.xls" but am struggling to be specific on the filename length.

    Can anyone advise?

    Thanks

    Mick
    Last edited by Telemick; 01-18-2012 at 05:42 AM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: open all files in a folder with fixed number of characters in filename

    Hi Telemick,
    And welcome to excel forum
    what is the code you are using?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    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: open all files in a folder with fixed number of characters in filename

    Hello telemick,

    Does the character count refer to the file name with or without the extension?
    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!)

  4. #4
    Registered User
    Join Date
    01-14-2012
    Location
    Darlington, England
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: open all files in a folder with fixed number of characters in filename

    Hi Pike,

    Apologies if this reply has been posted several times but I'm not sure if I'm driving this thing properly! Third try.

    Code is as follows

    Please Login or Register  to view this content.
    However, I want to open any file of the form 20120101,20120102 or alternatively 201201,201202 rather than all files called "Book*.xls" in the example code.

    Thanks

    Mick
    Last edited by pike; 01-15-2012 at 06:04 AM. Reason: \ in stead of / for newbie

  5. #5
    Registered User
    Join Date
    01-14-2012
    Location
    Darlington, England
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: open all files in a folder with fixed number of characters in filename

    Hi Leith,

    The 6 or 8 character filename does not include the file extension. In case it might be important, the files are excel comma separated values so the extension is csv.

    Apologies for not wrapping my code properly in my reply to Pike.

    Thanks for your interest Leith

    Mick
    Last edited by Telemick; 01-14-2012 at 05:24 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: open all files in a folder with fixed number of characters in filename

    Apologies for not wrapping my code properly in my reply to Pike.
    Please take a few minutes to read the forum rules and correct that.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    01-14-2012
    Location
    Darlington, England
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: open all files in a folder with fixed number of characters in filename

    I think this is the accepted format. Regards

    Please Login or Register  to view this content.
    Last edited by pike; 01-15-2012 at 06:03 AM. Reason: \ in stead of / for newbie

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: open all files in a folder with fixed number of characters in filename

    Hi Telemick
    I dont have FileSearch in xl 2010, but Steve may

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: open all files in a folder with fixed number of characters in filename

    FileSearch went away in Excel 2007, so maybe like this (untested):

    Please Login or Register  to view this content.
    Last edited by shg; 01-26-2012 at 08:49 PM. Reason: Corrected as pike pointed out

  10. #10
    Registered User
    Join Date
    01-14-2012
    Location
    Darlington, England
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: open all files in a folder with fixed number of characters in filename

    Hi SHG,

    Thanks for the code. I'll give it a try and let you know how I get on.

    Mick

  11. #11
    Registered User
    Join Date
    01-14-2012
    Location
    Darlington, England
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: open all files in a folder with fixed number of characters in filename

    Hi SHG,

    I've tried the code but come unstuck! It goes into an infinite loop between the Do While and Loop statements.

    Using the F8 command I stepped through it and found the first bit worked fine:

    Please Login or Register  to view this content.
    When the sfile line has run, the correct path and file name for the first file in the directory are shown when I hover the mouse over DIM sfile.

    The next bit of code is as follows:

    Please Login or Register  to view this content.
    Once the cursor has passed the "If spath & sfile..." line it jumps straight to "End if" without ever opening the file and now it is in a loop that it can't escape from.

    The other thing I noticed is that in the line

    Please Login or Register  to view this content.
    the value of ThisWorkbook.FullName is C/........Excel/XLSTART which is where the macro is stored. I have no idea if this what you intended.

    Thanks for your efforts so far. Just to confirm, it is Excel 2003 I am using (re the comment about filesearch gone in 2007)

    Best Regards

    Mick

  12. #12
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: open all files in a folder with fixed number of characters in filename

    Hi Telemick
    Move the sFile bit out side the if to just before the loop

    Please Login or Register  to view this content.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: open all files in a folder with fixed number of characters in filename

    Thanks, pike.

  14. #14
    Registered User
    Join Date
    01-14-2012
    Location
    Darlington, England
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: open all files in a folder with fixed number of characters in filename

    shg and pike - you have done it!

    Thanks a million.

    Mick

  15. #15
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: open all files in a folder with fixed number of characters in filename

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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