+ Reply to Thread
Results 1 to 11 of 11

Find File Path to USB Drive

  1. #1
    Registered User
    Join Date
    06-12-2009
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Find File Path to USB Drive

    Hi! I have a file which is kept on the C Drive. Each day, a different excel file is downloaded to a usb memory stick. The first file reads data from this file. The problem is, the drive letter keeps changing from E to F and the macro then crashes out.

    The file on the memory stick always has the same name, so is there a macro to find the drive letter of this file and then use it in my existing macro?

    Many thanks for anyone who can help!

  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: Find File Path to USB Drive

    Hello andyrads,

    Welcome to the Form!

    This macro will search all the local drives for the file, and returns either an empty string "" or the file name when the search is successful. Add your file folder path and the file name to the macro. These are marked in red. Copy this code into a standard VBA module.
    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
    Registered User
    Join Date
    06-12-2009
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Find File Path to USB Drive

    Hi! And thanks for the quick reply. I've copied and pasted the code into my current module, but this causes a syntax error "end sub expected". I think I'm doing something wrong?

  4. #4
    Registered User
    Join Date
    06-12-2009
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Find File Path to USB Drive

    Hi Leith Ross! I've tried again and put the code into a standard macro. I now get an error message "Invalid outside procedure". Could you put the code above into a complete macro for me to fiddle with? Many thanks for your help.

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Find File Path to USB Drive

    Here is another method.
    Please Login or Register  to view this content.

  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: Find File Path to USB Drive

    Hello andyrads,

    Sorry for the delay. I had to leave for awhile. The "Sub" statement should be "Function". I write more "Sub" procedures than functions and just automatically type "Sub". Also, I changed the code to start with drive "E", since "A-D" are generally hard drives.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-12-2009
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Find File Path to USB Drive

    Hiya! And once again thanks for the reply. I can get the function to run by calling it from a cell within the spreadsheet. But the result only gives me the name of the file I'm looking for..."test.xls". The file I'm trying to find on the usb memory stick has no path (it's not contained within a folder on the stick), it is just saved on it. The path is E:\[name of file], or whatever drive letter is assigned to it.

    Two things then, can I call the function from within a macro? and secondly, can this function be adapted to find the file and open it?

    Many thanks for all your help.

  8. #8
    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: Find File Path to USB Drive

    Hello andyrads,

    So, you need to be able to search for a given file name on all local disks that are removable and return the full path of the file if it is found. Did I miss anything?

  9. #9
    Registered User
    Join Date
    06-12-2009
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Find File Path to USB Drive

    Quote Originally Posted by Leith Ross View Post
    Hello andyrads,

    So, you need to be able to search for a given file name on all local disks that are removable and return the full path of the file if it is found. Did I miss anything?
    Hi again! Yep, you're right. But what I need is to either open the file "text.xls" when it is found or to have the drive letter as say x$ so that I can use that to point to the right drive. Hope all that makes sense and many thanks for you help. Just one other thing. Having never used a funtion before, how do I call or run the function from within a macro?

    Many thanks for all your help.

  10. #10
    Forum Contributor
    Join Date
    03-24-2009
    Location
    Vietnam
    MS-Off Ver
    Excel 2010
    Posts
    382

    Re: Find File Path to USB Drive

    OK, I cannot help you with the macro, but I would have a different solution. You wrote in your first post that the drive letter is always jumping between E and F.

    Lets say you want it to be always E, then the next time it jumps to F:

    Assuming you are using windows:
    1. Press START - RUN (or alternatively Windows-Key+R)
    2. enter "diskmgmt.msc" (without ")
    3. hit run
    4. select your USB stick
    5. right clickk - change drive letter
    6. select drive letter E from the list
    7. OK
    Done.

    The next time your stick is connected to that USB port it will be on E as well.

    Still, its just for your information, surely the macro helps you more

    greets,
    A2k

  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: Find File Path to USB Drive

    Hello andyrads,

    Finding a file by name on any drive is something that comes up rather frequently. I decided to bite the bullet and write the code. This code makes use of the File System Object which is part of the Microsoft Scripting Runtime Library. This first appeared in Excel 2000. If your version of Excel is earlier than 2000, the code won't run.

    The macro "FindFileNamed" has 2 arguments: the File Path and the File Name. The file path can be the root directory, like "C:\", a partial path which will check all sub directories, or a full path, or no path (an empty string). If no path is chosen then all local drives are checked. Successful search paths are saved in a Public Collection Object "PathsFound". The directory paths are available to all procedures because the object is public. The information only changes when the macro "FindFileNamed" is run.

    Macro Code
    Please Login or Register  to view this content.
    Examples
    Please Login or Register  to view this content.
    Setting a Reference in VBA
    1. Open your workbook and then press ALT+F11 to open the VBE.
    2. Press ALT+T followed by the Enter key.
    3. Scroll down the list until you find Microsoft Scripting Runtime Library.
    4. Press the Space bar to select it, and press Enter
    5. Press CTRL+S to save the reference in the project
    Last edited by Leith Ross; 06-15-2009 at 05:34 PM.

+ 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