473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How can I open all excel files in a folder and its sub folders

I get sent a folder containing zip files. I extract the zips to another folder. This then gives me 65 sub folders(directories), each with an excel and a text file. I wish to open all the excel files with one action in VB. If possible put them all on a single sheet in one workbook. They are all formatted the same and do not contain a lot of data so size and formatting will not be a problem.
I have
Expand|Select|Wrap|Line Numbers
  1. Sub Open_My_Files()
  2. Dim MyFile As String
  3. MyPath = "C:\Temp\Unzipped"
  4. MyFile = Dir(MyPath)
  5.  
  6. Do While MyFile <> ""
  7. If MyFile Like "*.xls" Then
  8. Workbooks.Open MyPath & MyFile
  9. End If
  10. MyFile = Dir
  11. Loop
  12. End Sub
to extract from a single folder. I need to get it out of sub folders though.
Can anyone please help?
Mar 18 '11 #1

✓ answered by Guido Geurs

This is tested in VBA Excel 2003
Q1: how to add Reference:(see gif's)
In VBA: click "Tools" - "References"
Select: Microsoft Scripting Runtime
Q2: The error is because the Reference is not added.

Attached is a demo sheet.

PS: if it does not work in 2007, please let me know.

8 8593
debasisdas
8,127 Expert 4TB
What about copying / moving all the files to a single location.
Mar 19 '11 #2
What about copying / moving all the files to a single location.
Because it would be faster to open them individually. I want to open 65 files quickly and copy them all to the same sheet.
Mar 21 '11 #3
Guido Geurs
767 Expert 512MB
This goes to a folder and search each subfolder for an XLS file.
You have to add the reference= Microsoft Scripting Runtime
(c:\windows\system32\scrrun.dll)

Expand|Select|Wrap|Line Numbers
  1. Private Sub Com_all_files_and_subs_Click()
  2. Dim FSO As New FileSystemObject
  3. Dim START_FOLDER As Folder
  4. Dim MyPath As String
  5.    MyPath = "C:\Temp\Unzipped"
  6.    MousePointer = 11
  7.    On Error GoTo Error_Files
  8.    Set START_FOLDER = FSO.GetFolder(MyPath)
  9.    Call Open_Files(START_FOLDER) '§ must be a function because it calls it's own
  10.    MousePointer = 0
  11. Exit Sub
  12. Error_Files:
  13.    MsgBox ("Error folder")
  14.    MousePointer = 0
  15. End Sub
  16.  
  17. Private Function Open_Files(ByVal START_FOLDER)
  18. Dim MyFile As File
  19. Dim SUBFOLDER As Folder
  20.    For Each MyFile In START_FOLDER.Files
  21.       On Error GoTo Volgende
  22.       Text1.Text = Text1.Text & MyFile & vbNewLine
  23.       If MyFile Like "*.xls" Then
  24.       Workbooks.Open MyFile
  25.       End If
  26. Volgende:
  27.    Next
  28.    For Each SUBFOLDER In START_FOLDER.SubFolders
  29.       Call Open_Files(SUBFOLDER)
  30.    Next
  31. End Function
Mar 21 '11 #4
Guido, a couple of problems. VB is not one of my strengths and I have little understanding of it.
What do I have to do for this
You have to add the reference= Microsoft Scripting Runtime
(c:\windows\system32\scrrun.dll)
Also this is "Private Sub " so it does not show on my list of available macros. I can change it to "Sub" then it does appear but I then get the error
Expand|Select|Wrap|Line Numbers
  1. Compile error
  2.  
  3. User defined type not defined
and this code is highlighted on the debug
Expand|Select|Wrap|Line Numbers
  1. Dim FSO As New FileSystemObject
Also this is in excel 2007, if that makes a difference
Mar 21 '11 #5
Guido Geurs
767 Expert 512MB
This is tested in VBA Excel 2003
Q1: how to add Reference:(see gif's)
In VBA: click "Tools" - "References"
Select: Microsoft Scripting Runtime
Q2: The error is because the Reference is not added.

Attached is a demo sheet.

PS: if it does not work in 2007, please let me know.
Mar 21 '11 #6
Thanks a lot Guido. I figured out the Tools and References but could not get the Private sub to run without your User Form. Works a treat in 2007.

Now for my next question, can VB copy all the first sheets in those books opened to a single sheet in a new workbook?

I'll start working on it but you will probably beat me to the answer.

Thanks again
Mar 21 '11 #7
I have so far managed
Expand|Select|Wrap|Line Numbers
  1. Sub CopyALL()
  2. ActiveSheet.Range("A1").CurrentRegion.Offset(1).Resize(Range("A1").CurrentRegion.Rows.Count - 1).Copy _
  3.  Destination:=Workbooks("copybook.xlsm").Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
  4. ActiveWorkbook.Close False
  5.  
  6. End Sub
Now I just need to get it to loop through this in your code Guido, if its possible. At the moment I have just associated it to a shortcut key so that I just keep pressing ctrl+g until I'm back to the "copybook" sheet where all the copies are.

[edit]Cracked it, goes between lines 24 and 25 of your code. Works a treat thanks[/edit]
Mar 22 '11 #8
Guido Geurs
767 Expert 512MB
this wiil collect all the first sheets and dump it in the workbook with the macro:(see also attachment)

Expand|Select|Wrap|Line Numbers
  1. Private Sub Com_all_files_and_subs_Click()
  2. Dim FSO As New FileSystemObject
  3. Dim START_FOLDER As Folder
  4. Dim MyPath As String
  5. Dim WORKBOOKSidx As Integer
  6. Dim ARRCOLLECT As Variant
  7.    MyPath = "C:\Temp\Unzipped"
  8.    MousePointer = 11
  9.    On Error GoTo Error_Files
  10.    Set START_FOLDER = FSO.GetFolder(MyPath)
  11.    Call OPEN_FILES(START_FOLDER) '§ must be a function because it calls it's own
  12.    For WORKBOOKSidx = 2 To Workbooks.Count
  13.       Workbooks(WORKBOOKSidx).Activate
  14.       Sheets(1).Activate
  15.       ReDim ARRCOLLECT(0) '§ clear array
  16.       '§ set sheet1 to array
  17.       ARRCOLLECT = Range("A1").Resize(Range("A1").End(xlDown).Row, _
  18.                                  Range("A1").End(xlToRight).Column)
  19.       '§ dump array in sheet "collect"
  20.       Workbooks(1).Activate
  21.       Sheets(1).Activate
  22.       If Range("A1") = "" Then '§ blanco sheet
  23.          Range("A1").Resize(UBound(ARRCOLLECT, 1), _
  24.                            UBound(ARRCOLLECT, 2)) = ARRCOLLECT
  25.       Else
  26.          Range("A" & Range("A1").End(xlDown).Row + 1). _
  27.                         Resize(UBound(ARRCOLLECT, 1), _
  28.                               UBound(ARRCOLLECT, 2)) = ARRCOLLECT
  29.       End If
  30.    Next
  31.    MousePointer = 0
  32. Exit Sub
  33. Error_Files:
  34.    MsgBox ("Error folder")
  35.    MousePointer = 0
  36. End Sub
  37.  
  38. Private Function OPEN_FILES(ByVal START_FOLDER)
  39. Dim MyFile As File
  40. Dim SUBFOLDER As Folder
  41.    For Each MyFile In START_FOLDER.Files
  42.       On Error GoTo Volgende
  43.       Text1.Text = Text1.Text & MyFile & vbNewLine
  44.       If MyFile Like "*.xls" Then Workbooks.Open MyFile
  45. Volgende:
  46.    Next
  47.    For Each SUBFOLDER In START_FOLDER.SubFolders
  48.       Call OPEN_FILES(SUBFOLDER)
  49.    Next
  50. End Function
Mar 22 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Karuppasamy | last post by:
Hi I want to populate all the files and folders of System in a Treeview control like Windows Explorer. I try this using File System Objects. But sometimes I am getting an error like 'Access...
0
by: Oci-One Kanubi | last post by:
Everything works fine in Access, but when I double-click on the resultant Excel files the first one opens correctly, but subsequent ones, and any other Excel files I try to open, fail to display at...
3
by: Rajiv Das | last post by:
VS 2003, XP SP2 ------------------------------------------------------------ DirectoryInfo temporary = new DirectoryInfo( Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));...
2
by: Neil | last post by:
Hi I'm rendering my datagrid to a HTML stream with a .xls extension to open in Excel however it opens in the browser. Is there a way to programatically force it to open Excel? I know you can...
2
by: Jon Maz | last post by:
Hi All, I'm in a possibly unusual situation, that is working alternately on an ASP.NET web site from two dev computers, one of which has VS.Net installed on it, the other of which does not. As...
6
by: Daniel | last post by:
Hi all, Can i open and edit the excel sheet on web page after downloading? After editing, i close the web page and the excel file auto upload to the server. Is it possible? I really struggling...
1
by: semedao | last post by:
Hi all, I'm looking for some example , open source etc code that implement caching of files and folders with those requirments: 1. like every file system , I can write , read , delete etc files...
11
by: karthi84 | last post by:
hi, if i have selected view hidden files and folders option from the folder option it is not enabled. at the time of applying it works but if i close that window and open it again its not...
6
by: chrisbirk | last post by:
Hi! I want to write an application on windows xp, which iterates n times a model script (exe) and copies every single run the results (multiple files such as text, maps, subfolders,etc) from the...
0
by: =?Utf-8?B?U3RldmU=?= | last post by:
I have a C# application (2005 .Net) which will loop thru files in a directory and read Excel files (currently using Excel 11). There are usually multiple Excel files in the directory. Up until a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.