+ Reply to Thread
Results 1 to 13 of 13

How to find files in a folder and write the path to the spreadsheet

  1. #1
    Registered User
    Join Date
    09-16-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    How to find files in a folder and write the path to the spreadsheet

    Hi, all.

    I am trying to locate files with certain extension, such as, jpg from a designated folder.

    I am using Excel2007, so I can't use FileSearch.

    For each file that the program finds, I want to write the full filename into a spreadsheet.

    Thank you for all your help in advance.

  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: How to find files in a folder and write the path to the spreadsheet

    This will put a list of JPG from the path you designate in the highlighted section into sheet1.
    Please Login or Register  to view this content.
    If you want the full path in the listing, then change this one line:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 09-24-2009 at 02:35 PM.
    _________________
    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
    09-16-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: How to find files in a folder and write the path to the spreadsheet

    Thank you JBeaucaire.

    But the macro is not outputting any file names in the spreadsheet.

    Is there a steps that I need to take in order for this macro to work?

    Thank you.

  4. #4
    Registered User
    Join Date
    09-16-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: How to find files in a folder and write the path to the spreadsheet

    Sorry for not waiting longer.
    I am in a short timeline and would greatly appreciate the fast help.
    Does anyone have a suggestion on why the above code does not work?
    OR does anyone have another code for the suggestion?
    Thank you all for your help.

  5. #5
    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: How to find files in a folder and write the path to the spreadsheet

    Hello KrNpRiDe,

    Here is another version. You can specify the folder path, file type, and where you want to output the file names.
    Please Login or Register  to view this content.
    Example
    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!)

  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: How to find files in a folder and write the path to the spreadsheet

    Quote Originally Posted by KrNpRiDe View Post
    Thank you JBeaucaire.

    But the macro is not outputting any file names in the spreadsheet.

    Is there a steps that I need to take in order for this macro to work?

    Thank you.
    As I indicated before the only thing you need to do is edit the path (marked in red) to the files you want listed before you ran the macro. Did you do that?

    How to use the macro:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Edit the path to the files you want listed
    6. Get out of VBA (Press Alt+Q)
    7. Save your sheet

    The macro is installed and ready to use. Press Alt-F8 and select it from the macro list.

    I just ran it again and it lists an entire directory, no problem.

  7. #7
    Registered User
    Join Date
    09-16-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: How to find files in a folder and write the path to the spreadsheet

    Thank you Leith Ross and JBeaucaire.

    I have tried both codes and does not work.

    JBeaucaire, your program runs without error, but does not output anything to the spreadsheet. The following is the code that I use to run.

    Please Login or Register  to view this content.

    Leith Ross, Your code is giving me an error:
    Run-time error '91': Object variable or With block variable not set

    I have modified the code a bit to suit my need, but it seems the error is setting the range.

    The following line is giving me the error.
    Dst = Sheet1.Range("A1")

    Please Login or Register  to view this content.
    Any solution to this issue will be appreciated.

    Once again, thank you so much for your help.

  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: How to find files in a folder and write the path to the spreadsheet

    When you go into the VBEditor and use F8 to step through my macro one line at a time, and you pass this line:
    Please Login or Register  to view this content.
    ...hover your mouse over the fName variable and what does it show? It should list the first jpg file in that directory.

    When I run it, I can see all 10 jpgs in my folder, one at a time of course.

    Something else you can check, make sure Excel is changing directories properly. When you pass this line of code:
    Please Login or Register  to view this content.
    ...switch back to the sheet and do a CTRL-O to open the current directory, are you in C:\Pictures? Can you filter for *.jpg and see .JPG files in this folder?

  9. #9
    Registered User
    Join Date
    09-16-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: How to find files in a folder and write the path to the spreadsheet

    Thank you for the quick response.

    When I am stepping through the macro, I can see that the fName stores the proper values.

    But it is not writing the name into the spreadsheet.

    Thanks.

  10. #10
    Registered User
    Join Date
    09-16-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: How to find files in a folder and write the path to the spreadsheet

    JBeaucaire, I was able to get an output.

    But it will only output, after stepping through the micro until the first output and then run the macro.

    Is there a way to fix that error, without having to step through the process everytime?

    I want to be able to run the program, without having to step through everytime I want to run this macro.

    Thank you.

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

    Re: How to find files in a folder and write the path to the spreadsheet

    If the macro works by stepping through the code, then it should work on its own. I can't imagine what might be interfering, it's pretty simple code.

    You could try commenting out (removing) all the lines of code having to do with OldDir and see if that was having any odd effects.

    Anyone else reading this see anything in Excel 2007 that might dislike any of the code? I've used these exact commands on many Excel 2007 macros.

  12. #12
    Registered User
    Join Date
    09-16-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: How to find files in a folder and write the path to the spreadsheet

    Thanks, JBeaucaire.

    I will try to erase that part of the code.

    Thank you all for great and fast responses.

  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: How to find files in a folder and write the path to the spreadsheet

    Hello KrNpRiDe,

    I am in agreement with JB that this related to 2007 as both his code and mine work correctly in 2003. Not having Excel 2007, I can't troubleshoot any problems in the code. To get an answer faster, you could create another thread addressing this specific problem. If you do, you should include a reference back to thread.

+ 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