Consulting

Results 1 to 10 of 10

Thread: Searching the Directory (Windows Explorer)

  1. #1
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location

    Searching the Directory (Windows Explorer)

    Okay another thing maybe you can help me with, I have always wondered if with VBA excel if you can search the entire directory to find a file and have that file and path be entered in cell ("A1"). Can this be done?

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Not sure if this will help you or if it what you want. You can search for one file or multiples.


    Sub GetImportFileName()
    '   Set up list of file filters
        Filt = "Text Files (*.txt),*.txt," & _
               "Lotus Files (*.prn),*.prn," & _
               "Comma Separated Files (*.csv),*.csv," & _
               "ASCII Files (*.asc),*.asc," & _
               "All Files (*.*),*.*"
    '   Display *.* by default
        FilterIndex = 5
    '   Set the dialog box caption
        Title = "Select a File to Import"
    '   Get the file name
        Filename = Application.GetOpenFilename _
            (FileFilter:=Filt, _
             FilterIndex:=FilterIndex, _
             Title:=Title)
    '   Exit if dialog box canceled
        If Filename = False Then
            MsgBox "No file was selected."
            Exit Sub
        End If
    '   Display full path and name of the file
        MsgBox "You selected " & Filename
    End Sub
    
    Sub GetImportFileName2()
    '   Set up list of file filters
        Filt = "Text Files (*.txt),*.txt," & _
                "Lotus Files (*.prn),*.prn," & _
                "Comma Separated Files (*.csv),*.csv," & _
                "ASCII Files (*.asc),*.asc," & _
                "All Files (*.*),*.*"
    '   Display *.* by default
        FilterIndex = 5
    '   Set the dialog box caption
        Title = "Select a File to Import"
    '   Get the file name
        Filename = Application.GetOpenFilename _
            (FileFilter:=Filt, _
             FilterIndex:=FilterIndex, _
             Title:=Title, _
             MultiSelect:=True)
    '   Exit if dialog box canceled
        If Not IsArray(Filename) Then
            MsgBox "No file was selected."
            Exit Sub
        End If
    '   Display full path and name of the files
        For i = LBound(Filename) To UBound(Filename)
            Msg = Msg & Filename(i) & vbCrLf
        Next i
        MsgBox "You selected:" & vbCrLf & Msg
    End Sub

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Oops. Forgot the code for putting it in cell A1. One sec.

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Modify this code for both the single and multiple file selections:

    Filename = Application.GetOpenFilename _
            (FileFilter:=Filt, _
             FilterIndex:=FilterIndex, _
             Title:=Title, _
             MultiSelect:=True)
             Sheet1.Range("A1") = Filename

  5. #5
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    Thank you, austenr
    I can figure how to put in cell "a1" this this is very helpful for another project I am working and I save all of this stuff as well. What I really need is to do a search like (Search Results) the same as if you went to your desktop went to Start/Search/Find All Files And Folders In Explorer Windows. Have the program search in the same fashion then place the file i am looking for maybe placed in a input box and have that file and path put in Cell "a1".

  6. #6
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    O.K. Glad to help.

  7. #7
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    You might also want to check out this link

    http://www.vba-programmer.com/VB_Cod...ingRuntime.txt

  8. #8
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    Your really helpful. Thanks again!

  9. #9
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    HTH. Take care

  10. #10
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi gsouza, and welcome to VBA Express!

    I'm not sure if you're aware of this, but if someone has answered your question to your satisfaction, you can mark your own threads solved! Just take a look in the 'Thread Tools" menu at the top of your post... it's a pretty great tool!

    I'll get this one.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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