Results 1 to 3 of 3

Thread: Problem with file search wild card

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Problem with file search wild card

    I am searching a directory for a filename using a wild card (*) in the file name. The problem is that the filesearch is picking up 2 files instead of one. for example, let's say there are 2 files in the folder named xFile.xls and File.xls. If I search for "File*.xls" it returns BOTH files when it should only be returning the File.xls. Below is an example of the code. Any ideas why the wildcard isn't working like it should?

    Code:
    Dim FsSearch As Office.FileSearch
    Set FsSearch = Application.FileSearch
    
    TempName = "File"
    
    With FsSearch
                .NewSearch
                .LookIn = ThisWorkbook.Path + "\"
                .FileName = TempName & "*.xls"
                .SearchSubFolders = True
                .FileType = msoFileTypeExcelWorkbooks
                .LastModified = msoLastModifiedAnyTime
                .Execute  'Count of how many records are found
    
                For Each vaFile In .FoundFiles
                        Workbooks.Open vaFile, False, False
                Next vaFile
    
    End With

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with file search wild card

    Would this help?

    vb Code:
    1. Sub ListAllFiles()
    2.     Dim sNextFile As String, MyPath As String, MyWildCard As String
    3.    
    4.     '~~> Path
    5.     MyPath = "C:\temp\"
    6.    
    7.     '~~> File Name
    8.     MyWildCard = "File"
    9.    
    10.     '~~> Developing the String
    11.     sNextFile = Dir$(MyPath & MyWildCard & "*.xls")
    12.    
    13.     '~~> Search files
    14.     While sNextFile <> ""
    15.         '~~> Display all names of file matching criteria
    16.         MsgBox sNextFile
    17.         sNextFile = Dir$
    18.     Wend
    19. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Problem with file search wild card

    I will have to try it and get back to you... a lot of strange things have been happening lately with my macros and it doesn't make sense. This code ran fine the other day but today it is a problem. I also had an Excel macro that had a masked edit control on it... it worked fine for the past few months, then one day when we opened the file the masked edit control had a big red "X" through it and it wouldn't work. I had to replace it with a text box. I'm just wondering if these problems are related to a bigger issue going on...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width