+ Reply to Thread
Results 1 to 13 of 13

Code to loop through workbooks in folder with specific names

  1. #1
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Code to loop through workbooks in folder with specific names

    Hello. How can I loop through workbooks with specific name order in a folder. I would like to use "ending with" as the identifier (for example, ending with RA first, TT second, etc). The last workbook may or may not be in the folder; the others will always be there. Thanks.

  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: Code to loop through workbooks in folder with specific names

    Hello stepme55,

    After the workbook is found, do you want to open it or do something else?
    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 Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Re: Code to loop through workbooks in folder with specific names

    Open it. Thanks.

  4. #4
    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: Code to loop through workbooks in folder with specific names

    Hello stephme55,

    This macro will prompt you for the folder that contains the workbooks you want to open by name. The string variable EndsWith filters the files in the folder using wildcard characters. Case is ignored. Each added filter is prefixed by a semi-colon. Aterisks (*) match any character, Question marks (?) any single character, And Pound (#) any single digit.

    If you have a fixed folder, the path can be added in to the macro after removing the Browse for Folder function. Let me know what you would like to do.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Re: Code to loop through workbooks in folder with specific names

    EndsWith = "*EFTPY.xls*;*REMIT.xls*";*FA.xls*";*RMT.xls*"

    This gives me an error.

  6. #6
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Re: Code to loop through workbooks in folder with specific names

    Changed it to this, but only opens up 1 file but not the rest.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Code to loop through workbooks in folder with specific names

    It is working fine for me
    Try editing the line to
    Please Login or Register  to view this content.
    And make sure the files is identical to this line .. for example : the file name would have any characters followed by EFTPY to be opened
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  8. #8
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Re: Code to loop through workbooks in folder with specific names

    Now it works thanks. Can you explain what it is doing exactly?

    I've never seen this folder, file stuff.

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Code to loop through workbooks in folder with specific names

    It opens all workbooks in the selected folder!!

  10. #10
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Re: Code to loop through workbooks in folder with specific names

    I know but I mean what do the different code lines mean; never seen some of those before.

  11. #11
    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: Code to loop through workbooks in folder with specific names

    Hello stephme55,

    Which lines of code need clarification?

  12. #12
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Re: Code to loop through workbooks in folder with specific names

    Hi Leith. All of them after "end with"

  13. #13
    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: Code to loop through workbooks in folder with specific names

    Hello stephme55,

    Here is the breakdown...

    CreateObject - Creates and returns a reference to a new instance of the specified ActiveX object. This macro creates a Shell Application object.

    BrowseForFolder - A Shell Application method to view and select folders using a customizable user interface.
    BrowseForFolder takes 4 arguments. The fourth is optional.
    The 1st is the Window handle to the parent window of the dialog box. Just uses zero for this argument.
    The 2nd is the Tile you want to display at the top of the dialog box.
    The 3rd argument is an Integer value that contains the options for displaying the dialog box. This can be zero or a combination of the values listed under the ulFlags member of the BROWSEINFO structure. In this macro 17 represents the following flags: BIF_RETURNONLYFSDIRS (1) + BIF_EDITBOX (16). The dialog box will list only directories (Folders) and provide a text box for the user to type in a folder path.
    The 4th argument is the root folder to use in the dialog box. The user cannot browse higher in the tree than this folder. If this value is not specified, the root folder used is the desktop. This value can be a string that specifies the path of the folder or one of the ShellSpecialFolderConstants values.
    Namespace - A Namespace is used to create the scope for organizing code elements and to create globally unique types. In this macro the Namespace is the Folder.
    The Namespace takes a single argument of the Type Variant. A Namespace in Windows can be a string or a numeric Shell Special Folder constant or a GUID.
    Folder.Items - Returns the collection of File, Folders, and Links within a folder.

    Filter - A method to select only the Folder Items of interest.
    Filter take 2 arguments: Shell Constant Flag, Filter String. There are only 2 flags that are useful for files and folders: SHCONTF_FOLDERS = 32 and SHCONTF_NONFOLDERS = 64. The filter syntax follows the same rules as the filter in Application.GetOpenFilename.

+ 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] Loop Through Folder, Create Emails with Sub Folder Names in Subject, Attach files in sub
    By Rschwar23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2015, 10:06 AM
  2. Create FOLDER in subfoldesr and move folders with specific names into FOLDER
    By Amarjeet Singh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2015, 09:46 AM
  3. Replies: 1
    Last Post: 10-18-2014, 05:04 PM
  4. [SOLVED] Loop Through all workbooks in a Folder
    By ag273n in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-21-2014, 02:17 PM
  5. Loop Through a Folder of Excel Workbooks
    By frankgbr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-01-2013, 12:18 AM
  6. For loop which goes through all workbooks in a folder
    By secondrate in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-07-2010, 02:49 PM
  7. Loop through folder of workbooks and add rows
    By FIRSTROUNDKO via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-10-2006, 02:55 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