PDA

View Full Version : Solved: Searching the Directory (Windows Explorer)



gsouza
12-17-2004, 08:41 AM
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?

austenr
12-17-2004, 09:57 AM
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

austenr
12-17-2004, 09:58 AM
Oops. Forgot the code for putting it in cell A1. One sec.

austenr
12-17-2004, 10:03 AM
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

gsouza
12-17-2004, 10:10 AM
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".

austenr
12-17-2004, 10:26 AM
O.K. Glad to help.

austenr
12-17-2004, 10:32 AM
You might also want to check out this link

http://www.vba-programmer.com/VB_Code/Finding_File_using_SriptingRuntime.txt

gsouza
12-17-2004, 10:40 AM
Your really helpful. Thanks again!

austenr
12-17-2004, 11:06 AM
HTH. Take care

Ken Puls
12-18-2004, 04:19 PM
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!:yes

I'll get this one. ;)