Opening files in a folder

  • Thread starter Thread starter Daniel Bonallack
  • Start date Start date
D

Daniel Bonallack

Could someone please tell me how to open all the Excel
files in specific folder, via code?

For example, I have files A.xls, B.xls and C.xls in the
folder C:\VBA\OpenFiles\, along with several Word files.
What code would open these three files?

Thanks in advance

Daniel
 
Sub dirreader()
YourPath = "c:\VBA\OpenFiles\,*.xls"
name1 = Dir(YourPath, vbDirectory)
Do While name1 <> ""
If name1 <> "." And name1 <> ".." Then
MsgBox name1
End If
name1 = Dir
Loop


End Su
 
Try this one Daniel

Change the folder to yours

Sub Test()
Dim mybook As Workbook
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String

SaveDriveDir = CurDir
MyPath = "C:\Data"
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xls")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If

Application.ScreenUpdating = False
Do While FNames <> ""
Set mybook = Workbooks.Open(FNames)
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub
 
Thank you for your replies (three different but equally
efficient solutions).

regards
Daniel
 

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

Back
Top