Finding specific excel files

  • Thread starter Thread starter ucanalways
  • Start date Start date
U

ucanalways

I have the following to find files in a folder with .xls extension.

Dim MyPath As String
FilesInPath = Dir(MyPath & "*.xls")

How would tweak this to find excel files that start with "1_"?

Any help would be much appreciated!

Kevin
 
I'd use:

Option Explicit
Sub testme01()

Dim myNames() As String
Dim fCtr As Long
Dim myFile As String
Dim myPath As String
Dim TempWkbk as Workbook

'use whatever you know to get the folder
myPath = "C:\my documents\excel\test\"
If myPath = "" Then Exit Sub
If Right(myPath, 1) <> "\" Then
myPath = myPath & "\"
End If

myFile = ""
On Error Resume Next
myFile = Dir(myPath & "*.xls")
On Error GoTo 0
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If

'get the list of files
fCtr = 0
Do While myFile <> ""
If LCase(myFile) Like LCase("1_*.xls") Then
fCtr = fCtr + 1
ReDim Preserve myNames(1 To fCtr)
myNames(fCtr) = myFile
End If
myFile = Dir()
Loop

If fCtr > 0 Then
For fCtr = LBound(myNames) To UBound(myNames)
Set TempWkbk = Workbooks.Open(Filename:=myPath & myNames(fCtr))
'do some stuff
TempWbk.Close savechanges:=False 'or True
Next fCtr
End If

End Sub
 
There was a typo:
Change:
TempWbk.Close savechanges:=False 'or True
to:
TempWkbk.Close savechanges:=False 'or True
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top