+ Reply to Thread
Results 1 to 11 of 11

Find File Path to USB Drive

Hybrid View

  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,259

    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.
    Function SearchDrives() As String
    
      Dim DriveLetter As String
      Dim FileName As String
      Dim FilePath As String
      Dim FN As String
      Dim I As Integer
      
        FilePath = "Documents and Settings\Owner\My Documents"
        FileName = "Test.xls"
        
        For I = 1 To 26
          DriveLetter = Chr$(64 + I) & ":\"
          On Error GoTo FileError
          FN = Dir(DriveLetter & FilePath & "\" & FileName)
          If StrComp(FN, FileName, vbTextCompare) = 0 Then
             SearchDrives = FileName
             Exit Sub
          End If
    GetNextDrive:
        Next I
            
      Exit Function
      
    FileError:
        If Err = 52 Then
           Err.Clear
           Resume GetNextDrive
        Else
           On Error GoTo 0
           Resume
        End If
      
    End Function
    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.
    Sub Test_TheFileName()
      Dim sFilename As String
      
      sFilename = ThisWorkbook.FullName
      sFilename = TheFileName(sFilename, "")
      MsgBox sFilename, , "Guess1"
      
      sFilename = ThisWorkbook.FullName
      sFilename = TheFileName("", sFilename)
      MsgBox sFilename, , "Guess2"
    
      sFilename = TheFileName("f:\myfiles\excel\ken.xls", "g:\myfiles\excel\ken.xls")
      MsgBox sFilename
      
    End Sub
    
    Function TheFileName(guess1 As String, guess2 As String) As String
      Select Case True
        Case Dir(guess1) <> "" And guess1 <> ""
          TheFileName = guess1
        Case Dir(guess2) <> "" And guess2 <> ""
          TheFileName = guess2
      End Select
    End Function

  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,259

    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.
    Function SearchDrives() As String
    
      Dim DriveLetter As String
      Dim FileName As String
      Dim FilePath As String
      Dim FN As String
      Dim I As Integer
      
        FilePath = "Documents and Settings\Owner\My Documents"
        FileName = "Test.xls"
        
       'Search drives "E" through "Z"    
        For I = 5 To 26
          DriveLetter = Chr$(64 + I) & ":\"
          On Error GoTo FileError
          FN = Dir(DriveLetter & FilePath & "\" & FileName)
          If StrComp(FN, FileName, vbTextCompare) = 0 Then
             SearchDrives = FileName
             Exit Function
          End If
    GetNextDrive:
        Next I
            
      Exit Function
      
    FileError:
        If Err = 52 Then
           Err.Clear
           Resume GetNextDrive
        Else
           On Error GoTo 0
           Resume
        End If
      
    End Function

+ 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