Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Minerva
 
Posts: n/a
Default Iterating files in folder

I am consolidating data from a group of files-the # may keep
increasing/varying.
I want to create a macro that-
asks the user to specify the folder where all these files are located;
once user selects folder, the macro should iterate thru each file and copy
corresponding values onto one master file.
The FileDialogue function doesn't help much...any other function that can
ask folder name and open each file one by one by itself?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Bob Phillips
 
Posts: n/a
Default Iterating files in folder

Here is one way

Option Explicit

Dim oFSO As Object
Dim oWks As Worksheet

Sub LoopFolders()
Dim sStartFolder As String

With Application.FileDialog(msoFileDialogFolderPicker)
.Show
On Error Resume Next
sStartFolder = .SelectedItems(1)
On Error GoTo 0
End With

If sStartFolder < "" Then
Set oWks = ActiveSheet
Set oFSO = CreateObject("Scripting.FileSystemObject")

selectFiles "c:\MyTest"

Set oFSO = Nothing
End If

End Sub


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

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
iLastRow = oWks.Cells(Rows.Count, "A").End(xlUp).Row
If iLastRow < 1 Or oWks.Range("A1").Value < "" Then
iLastRow = iLastRow + 1
End If
activehseet.Range("A1:A10").Copy _
Destination:=oWks.Range("A" & iLastRow)
End If
Next file

End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Minerva" wrote in message
...
I am consolidating data from a group of files-the # may keep
increasing/varying.
I want to create a macro that-
asks the user to specify the folder where all these files are located;
once user selects folder, the macro should iterate thru each file and copy
corresponding values onto one master file.
The FileDialogue function doesn't help much...any other function that can
ask folder name and open each file one by one by itself?

Thanks



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
Periodically listing files in a folder haven104 Excel Discussion (Misc queries) 2 January 10th 06 09:01 AM
Unable to load files from alternate startup folder Bill.Carlson Excel Discussion (Misc queries) 3 November 24th 05 03:36 PM
Export html page to Excel - support files folder Export html page to Excel - support file Excel Discussion (Misc queries) 1 June 24th 05 12:36 PM
add same cell from all files in same folder Richard Varcoe Excel Discussion (Misc queries) 1 May 23rd 05 02:24 PM
Copy files from spreadsheet into folder Intotao Excel Discussion (Misc queries) 3 January 27th 05 11:38 PM


All times are GMT +1. The time now is 12:25 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"