ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   User selection of folder and open all .xls files within folder (https://www.excelbanter.com/excel-programming/387419-user-selection-folder-open-all-xls-files-within-folder.html)

Barb Reinhardt

User selection of folder and open all .xls files within folder
 
I need to allow the user to select a folder so that .xls files meeting a
specific file naming convention can be opened. I know how to create an
array of workbooks based upon that naming convention, but I've not been
successful in allowing the user to select a folder. How should I code this?

Thanks,
Barb Reinhardt

Norman Jones

User selection of folder and open all .xls files within folder
 
Hi Barb,

'-----------------
I need to allow the user to select a folder so that .xls files meeting a
specific file naming convention can be opened. I know how to create an
array of workbooks based upon that naming convention, but I've not been
successful in allowing the user to select a folder. How should I code
this?
'-----------------

See John Walkenbach at:

Selecting a Directory
http://j-walk.com/ss/excel/tips/tip29.htm

See also Jim Rech's BrowseForFolder download which
may be freely downloaded from Stephen Bullen's
OfficeAutomation site:

http://www.bmsltd.ie/MVP/Default.htm

Also visit Chip Pearson at:

Selecting A Folder With VBA
http://www.cpearson.com/excel/BrowseFolder.htm



---
Regards,
Norman



Bob Phillips

User selection of folder and open all .xls files within folder
 
Here is one simple way


Sub LoopFolders()

Set oFSO = CreateObject("Scripting.FileSystemObject")

With Application.FileDialog(msoFileDialogFolderPicker)
.Show

selectFiles .SelectedItems(1)

End With

Set oFSO = Nothing

End Sub


'---------------------------------------------------------------------------
Sub selectFiles(sPath)
'---------------------------------------------------------------------------
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fldr

Set Folder = oFSO.GetFolder(sPath)

For Each fldr In Folder.Subfolders
selectFiles fldr.Path
Next fldr

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
End If
Next file

End Sub



--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Barb Reinhardt" wrote in message
...
I need to allow the user to select a folder so that .xls files meeting a
specific file naming convention can be opened. I know how to create an
array of workbooks based upon that naming convention, but I've not been
successful in allowing the user to select a folder. How should I code
this?

Thanks,
Barb Reinhardt




Dave Peterson

User selection of folder and open all .xls files within folder
 
Just an addendum to Bob's reply:

Application.FileDialog was added in xl2002 (IIRC).

And I think Bob wanted to declare oFSO at the top of the module.


Bob Phillips wrote:

Here is one simple way

Sub LoopFolders()

Set oFSO = CreateObject("Scripting.FileSystemObject")

With Application.FileDialog(msoFileDialogFolderPicker)
.Show

selectFiles .SelectedItems(1)

End With

Set oFSO = Nothing

End Sub

'---------------------------------------------------------------------------
Sub selectFiles(sPath)
'---------------------------------------------------------------------------
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fldr

Set Folder = oFSO.GetFolder(sPath)

For Each fldr In Folder.Subfolders
selectFiles fldr.Path
Next fldr

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
End If
Next file

End Sub

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Barb Reinhardt" wrote in message
...
I need to allow the user to select a folder so that .xls files meeting a
specific file naming convention can be opened. I know how to create an
array of workbooks based upon that naming convention, but I've not been
successful in allowing the user to select a folder. How should I code
this?

Thanks,
Barb Reinhardt


--

Dave Peterson

Bob Phillips

User selection of folder and open all .xls files within folder
 
I did indeed. my cut failed me.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Dave Peterson" wrote in message
...
Just an addendum to Bob's reply:

Application.FileDialog was added in xl2002 (IIRC).

And I think Bob wanted to declare oFSO at the top of the module.


Bob Phillips wrote:

Here is one simple way

Sub LoopFolders()

Set oFSO = CreateObject("Scripting.FileSystemObject")

With Application.FileDialog(msoFileDialogFolderPicker)
.Show

selectFiles .SelectedItems(1)

End With

Set oFSO = Nothing

End Sub

'---------------------------------------------------------------------------
Sub selectFiles(sPath)
'---------------------------------------------------------------------------
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fldr

Set Folder = oFSO.GetFolder(sPath)

For Each fldr In Folder.Subfolders
selectFiles fldr.Path
Next fldr

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
End If
Next file

End Sub

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my
addy)

"Barb Reinhardt" wrote in
message
...
I need to allow the user to select a folder so that .xls files meeting a
specific file naming convention can be opened. I know how to create
an
array of workbooks based upon that naming convention, but I've not been
successful in allowing the user to select a folder. How should I code
this?

Thanks,
Barb Reinhardt


--

Dave Peterson





All times are GMT +1. The time now is 05:21 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com