+ Reply to Thread
Results 1 to 6 of 6

Reading in the files in a directory into an array, directory chosen by user

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Reading in the files in a directory into an array, directory chosen by user

    HI,

    I have some qty of excel files. All the files are the same except for the name. I need to open each file and copy the data. I will paste the data from each file into a single file.

    What i was wondering is does anyone have a good program to do the following:

    1) User selects a diretory
    2) After directory selected you populate the array with all the excel files in the directory

    any help would be appreaciated. 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: Reading in the files in a directory into an array, directory chosen by user

    Hello welchs101,

    Here is macro to allow the user to select a directory and return all ".xls" files in the folder. If no Excel files are found then a message is displayed indicating this and the macro quits.
    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 Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Reading in the files in a directory into an array, directory chosen by user

    Hi Leith Ross,

    Nice! Works great!!!!!!!!!!!!!!!!!

    Thanks alot!

    Few quick questions:

    1) To call this function do i have to create an array of type variant or can it be an array of type string?

    2) The entries in the array have the full "Path" as well as the actual file. How do i strip out just the file itself from the string..........if you know a quick fix please let me know. The stripping out the file name only can be done in a separate array if you want.



    Thanks again........your soln works great!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Reading in the files in a directory into an array, directory chosen by user

    Just noticed that when i put this into my actual program.........i get an error. Apparently in my program i have at the top of the macro

    Option Base 1


    Apparently in this program your starting at "0" .....how to change your code so that it starts from "1"?

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Reading in the files in a directory into an array, directory chosen by user

    ok...i just added cnt=1 prior to the Do loop

    Please Login or Register  to view this content.
    and now it starts at 1.

    thanks.

  6. #6
    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: Reading in the files in a directory into an array, directory chosen by user

    Hello Welchs101,

    The array passed back the function is a Variant that contains strings. If there are no files found then the return value will be "empty". The returned array is a 1-D array.

    Here is an example of listing the returned files in column "A" of the active sheet:
    Please Login or Register  to view this content.

    Updated Macro
    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