Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Searching a filename from multiple workbooks or drives


Does anyone know how to write a simple code searching for a filename or
just a name from several workbooks & different drives by just only a
click? Thanks in advance.

John


--
jt46
------------------------------------------------------------------------
jt46's Profile: http://www.excelforum.com/member.php...o&userid=15154
View this thread: http://www.excelforum.com/showthread...hreadid=268367

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Searching a filename from multiple workbooks or drives

If you need to search only a single directory, the code is simple. However, if
you need to search the directory tree, including any subdirectories under the
top directory, and subdirectories under those subdirectories, etc, it is not
trivial.

In the past, Bill Manville posted the code below to list all files of a
specified type in and below a top level directory. Maybe you can figure out
how to modify it for your purpose. The basic change would be that you must
pass to the routine the file name you're looking for and to save a file name
ONLY if it's a match. It would be up to you to continue or discontinue when
the first file is found.

The first sub is a demo. It puts the file names on Sheet1.

Option Base 1
Dim aFiles() As String, iFile As Integer

Sub ListAllFilesInDirectoryStructure()
Dim Counter As Integer
iFile = 0
ListFilesInDirectory "c:\test\" ' change the top level as you wish

For Counter = 1 To iFile
Worksheets("Sheet1").Cells(Counter, 1).Value = aFiles(Counter)
Next

End Sub

Sub ListFilesInDirectory(Directory As String)
Dim aDirs() As String, iDir As Integer, stFile As String

' use Dir function to find files and directories in Directory
' look for directories and build a separate array of them
' Dir returns files as well as directories when vbDirectory specified
iDir = 0
stFile = Directory & Dir(Directory & "*.*", vbDirectory)
Do While stFile < Directory
If Right(stFile, 2) = "\." Or Right(stFile, 3) = "\.." Then
' do nothing - GetAttr doesn't like these directories
ElseIf GetAttr(stFile) = vbDirectory Then
' add to local array of directories
iDir = iDir + 1
ReDim Preserve aDirs(iDir)
aDirs(iDir) = stFile
Else
' add to global array of files
'MAKE CHANGE HERE TO SAVE THE NAME ONLY IF IT MATCHES
iFile = iFile + 1
ReDim Preserve aFiles(iFile)
aFiles(iFile) = stFile
End If
stFile = Directory & Dir()
Loop

' now, for any directories in aDirs call self recursively
If iDir 0 Then
For iDir = 1 To UBound(aDirs)
ListFilesInDirectory aDirs(iDir) & Application.PathSeparator
Next iDir
End If
End Sub

On Mon, 11 Oct 2004 22:05:47 -0500, jt46
wrote:


Does anyone know how to write a simple code searching for a filename or
just a name from several workbooks & different drives by just only a
click? Thanks in advance.

John


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching Multiple Excel Workbooks G.Simpson Excel Discussion (Misc queries) 3 December 21st 06 04:09 PM
Searching Multiple Workbooks ClarisOLeary via OfficeKB.com Excel Discussion (Misc queries) 2 October 12th 06 05:01 PM
Searching multiple workbooks phreshjive Excel Discussion (Misc queries) 2 January 26th 06 06:56 PM
Opening or modifying multiple workbooks with same filename Matt Lawson[_2_] Excel Programming 3 March 2nd 04 01:47 AM
Trying to Open Multiple WorkBooks w/ same beginning FileName Hambone[_2_] Excel Programming 2 February 17th 04 05:36 PM


All times are GMT +1. The time now is 12:04 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"