+ Reply to Thread
Results 1 to 9 of 9

Search files in folder based on search string

Hybrid View

  1. #1
    Registered User
    Join Date
    12-25-2004
    Posts
    61

    Search files in folder based on search string

    I have several XLS files in a folder. They are all name similarly:

    2111-13252 (1-5-08)
    2111-12345 (5-32-07)
    1122-21935 (9-5-08)

    Location - Account (Date)

    In Excel, I want to be able to search for a file. Bring up a popup where the user types in the 5 digit account number [MID(7,5)]. Code searches all files for that account number. If it finds it, then create MsgBox with the entire filename.

    So the user types in 21935 in the popup.
    Code looks for these digits in the folder, based off characters 6 - 10 of the filenames in that folder.
    When it finds it, display filename in a MsgBox, so in this case "1122-21935 (9-5-08).xls"

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

    Did you want to search a specific directory or multiple directories?

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    12-25-2004
    Posts
    61
    Single directory.

    After pondering it some more, I think it would be best to list the filenames in Sheet1 under column A. That way, if there are more than 1 results, it can list all files with the search criteria.

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

    This macro will start listing files in the specified directory that match the account number input by the user. The list starts on Sheet1 A1. You can change these if you need to. They are marked in red.
    Sub ListFileNames()
    
      Dim FileName As String
      Dim FilePath As String
      Dim Valid As Boolean
      Dim Wks As Worksheet
      
        R = 1    'Starting Row
        Set Wks = Worksheets("Sheet1")
        FilePath = "C:\Documents and Settings\Owner\My Documents\*.xls"
        
    InputAcct:
         Acct = InputBox("Please Enter an Account number.")
           If Acct = "" Then Exit Sub
             Valid = Acct Like "#####"
             If Not Valid Then
               MsgBox "Please Enter a 5 digit Account number."
               GoTo InputAcct
             End If
           
         FileName = Dir(FilePath)
         
         Do While FileName <> ""
           FileName = Dir()
           Valid = FileName Like "####-##### ("
             If Valid Then
               If Mid(FileName, 6, 5) = Acct Then
                 Wks.Cells(R, "A") = FileName
                 R = R + 1
               End If
             End If
         Loop
         
    End Sub
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 09-21-2008 at 03:46 PM.

  5. #5
    Registered User
    Join Date
    12-25-2004
    Posts
    61
    Hmmm....im trying to run the code but it doesn't do anything.

  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
    Hello djvice,

    Sorry about that, I made a typo in the Like operator for the file name comparison. It is missing an asterisk at the end.
      Valid = FileName Like "####-##### (*"
    Here is the revised code...
    Sub ListFileNames()
    
      Dim FileName As String
      Dim FilePath As String
      Dim Valid As Boolean
      Dim Wks As Worksheet
      
        R = 1    'Starting Row
        Set Wks = Worksheets("Sheet1")
        FilePath = "C:\Documents and Settings\Admin.ADMINS\My Documents\*.xls"
        
    InputAcct:
         Acct = InputBox("Please Enter an Account number.")
           If Acct = "" Then Exit Sub
             Valid = Acct Like "#####"
             If Not Valid Then
               MsgBox "Please Enter a 5 digit Account number."
               GoTo InputAcct
             End If
           
         FileName = Dir(FilePath)
         
         Do While FileName <> ""
           Valid = FileName Like "####-##### (*"
             If Valid Then
               If Mid(FileName, 6, 5) = Acct Then
                 Wks.Cells(R, "A") = FileName
                 R = R + 1
               End If
             End If
           FileName = Dir
         Loop
         
    End Sub
    Sincerely,
    Leith Ross

  7. #7
    Registered User
    Join Date
    12-25-2004
    Posts
    61
    You're a freakin' genius!

  8. #8
    Registered User
    Join Date
    12-25-2004
    Posts
    61
    How do I add a Msgbox if no files were found?
    Last edited by djvice; 09-22-2008 at 12:55 AM. Reason: N/m, figured it out ;)

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello djvice,

    At the end of the loop, check if "R" = 1. R increments each time a file is found. If R = 1 then no files were found.

    Sincerely,
    Leith Ross

+ 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. Default folder for saving files
    By Karamazov in forum Excel General
    Replies: 3
    Last Post: 06-09-2008, 03:22 PM
  2. Excel files replicating and renaming in a folder
    By hpum in forum Excel General
    Replies: 0
    Last Post: 07-21-2007, 09:23 AM
  3. Apply the same macro code to all excel files in a folder
    By Alano in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-09-2007, 09:26 AM
  4. How do to search for a string in excel across all rows/columns and save the result?
    By akondeti in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-07-2006, 07:16 PM
  5. ¿How could i search a string from Book 1 into Book2?
    By Actarus in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-27-2006, 02:21 AM

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